Skip to main content
2 de 3
se añadieron 848 caracteres en el cuerpo
Jorgesys
  • 111.1k
  • 14
  • 55
  • 134

Error al abrir la base de datos Android studio

estoy haciendo una aplicacion donde uso una base de datos.Me salta este error

02-16 22:53:25.979 19587-19587/dev.mros.espanyaapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{dev.mros.espanyaapp/dev.mros.espanyaapp.Playing}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
                                                                     at android.app.ActivityThread.access$700(ActivityThread.java:159)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                     at android.os.Looper.loop(Looper.java:176)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5419)
                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:525)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
                                                                     at dalvik.system.NativeStart.main(Native Method)
                                                                  Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
                                                                     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
                                                                     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:339)
                                                                     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:236)
                                                                     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:515)
                                                                     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:207)
                                                                     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
                                                                     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:891)
                                                                     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:862)
                                                                     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
                                                                     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
                                                                     at dev.mros.espanyaapp.DbHelper.DbHelper.openDataBase(DbHelper.java:46)
                                                                     at dev.mros.espanyaapp.DbHelper.DbHelper.<init>(DbHelper.java:39)
                                                                     at dev.mros.espanyaapp.Playing.onCreate(Playing.java:53)
                                                                     at android.app.Activity.performCreate(Activity.java:5372)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
                                                                     at android.app.ActivityThread.access$700(ActivityThread.java:159) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                     at android.os.Looper.loop(Looper.java:176) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5419) 
                                                                     at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                     at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862) 
                                                                     at dalvik.system.NativeStart.main(Native Method) 

No consigo solucionarlo ya que nunca me ha pasado esto. Me salta el error en este metodo

    public void openDataBase() {
    String myPath = DB_PATH + DB_NAME;
    mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}

Os dejo por aqui mi base de datos

    public class DbHelper extends SQLiteOpenHelper {

    private static String DB_NAME = "MyDB.db";
    private static String DB_PATH = "";
    private SQLiteDatabase mDataBase;
    private Context mContext = null;

    public DbHelper(Context context) {
        super(context, DB_NAME, null, 1);

        DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
        File file = new File(DB_PATH+"MyDB.db");
        if(file.exists())
            openDataBase();
        this.mContext = context;

    }

    public void openDataBase() {
        String myPath = DB_PATH + DB_NAME;
        mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    public void copyDataBase() throws IOException {
        try {
            InputStream myInput = mContext.getAssets().open(DB_NAME);
            String outputFileName = DB_PATH + DB_NAME;
            OutputStream myOutput = new FileOutputStream(outputFileName);

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0)
                myOutput.write(buffer, 0, length);

            myOutput.flush();
            myOutput.close();
            myInput.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean checkDataBase() {
        SQLiteDatabase tempDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            tempDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLiteException e) {
            e.printStackTrace();
        }
        if (tempDB != null)
            tempDB.close();
        return tempDB != null ? true : false;
    }

    public void createDataBase() throws IOException {
        boolean isDBExists = checkDataBase();
        if (isDBExists) {

        } else {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    //Question (PREGUNTA) *BASE DE DATOS*
    public List<Question> getAllQuestion() {
        List<Question> listQuestion = new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c;
        try {
            c = db.rawQuery("SELECT * FROM Question ORDER BY Random()", null);
            if (c == null) return null;
            c.moveToFirst();
            do {
                int Id = c.getInt(c.getColumnIndex(mContext.getString(R.string.IDdb)));
                String Image = c.getString(c.getColumnIndex(mContext.getString(R.string.IMAGEdb)));
                String AnswerA = c.getString(c.getColumnIndex(mContext.getString(R.string.ANSWERAdb)));
                String AnswerB = c.getString(c.getColumnIndex(mContext.getString(R.string.ANSWERBdb)));
                String AnswerC = c.getString(c.getColumnIndex(mContext.getString(R.string.ANSWERCdb)));
                String AnswerD = c.getString(c.getColumnIndex(mContext.getString(R.string.ANSWERDdb)));
                String CorrectAnswer = c.getString(c.getColumnIndex(mContext.getString(R.string.CorrectAnswerdb)));

                Question question = new Question(Id, Image, AnswerA, AnswerB, AnswerC, AnswerD, CorrectAnswer);
                listQuestion.add(question);
            }
            while (c.moveToNext());
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        db.close();
        return listQuestion;
    }

    //Tipos de NIVELES
    public List<Question> getQuestionMode(String mode) {
        List<Question> listQuestion = new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor c;
        int limit = 0;
        if (mode.equals(Common.MODE.FACIL.toString()))
            limit = 6;
        else if (mode.equals(Common.MODE.MEDIO.toString()))
            limit = 12;
        else if (mode.equals(Common.MODE.DIFICIL.toString()))
            limit = 27;
        else if (mode.equals(Common.MODE.DIFICILISOMO.toString()))
            limit = 50;
        try {
            c = db.rawQuery(String.format("SELECT * FROM Question ORDER BY Random() LIMIT %d", limit), null);
            if (c == null) return null;
            c.moveToFirst();
            do {
                int Id = c.getInt(c.getColumnIndex(mContext.getString(R.string.ID2db)));
                String Image = c.getString(c.getColumnIndex(mContext.getString(R.string.IMAGE2db)));
                String AnswerA = c.getString(c.getColumnIndex(mContext.getString(R.string.ANSWER2db)));
                String AnswerB = c.getString(c.getColumnIndex(mContext.getString(R.string.ANSWERB2db)));
                String AnswerC = c.getString(c.getColumnIndex(mContext.getString(R.string.ANSWERC2db)));
                String AnswerD = c.getString(c.getColumnIndex(mContext.getString(R.string.ANSWERD2db)));
                String CorrectAnswer = c.getString(c.getColumnIndex(mContext.getString(R.string.CorrectAnswer2db)));

                Question question = new Question(Id, Image, AnswerA, AnswerB, AnswerC, AnswerD, CorrectAnswer);
                listQuestion.add(question);
            }
            while (c.moveToNext());
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        db.close();
        return listQuestion;
    }

    //Un insert para el RANKING
    public void insertScore(double score) {
        String query = "INSERT INTO Ranking(Score) VALUES("+score+")";
        mDataBase.execSQL(query);
    }

    public void updatePlayCount(int level,int playCount)
    {
        String query = String.format("UPDATE UserPlayCount Set PlayCount = %d WHERE Level = %d",playCount,level);
        mDataBase.execSQL(query);
    }

    public int getPlayCount(int level)
    {
        int result = 0;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c;
        try{
            c = db.rawQuery("SELECT PlayCount FROM UserPlayCount WHERE Level="+level+";",null);
            if(c == null) return 0;
            c.moveToNext();
            do{
                result  = c.getInt(c.getColumnIndex(mContext.getString(R.string.PLAYCOUNTdb)));
            }while(c.moveToNext());
            c.close();
        }catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return result;
    }

    //Obtener el Score (PUNTUACION)
    public List<Ranking> getRanking() {
        List<Ranking> listRanking = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c;
        try {
            c = db.rawQuery("SELECT * FROM Ranking Order By Score DESC;", null);
            if (c == null) return null;
            c.moveToNext();
            do {
                int Id = c.getInt(c.getColumnIndex(mContext.getString(R.string.ID3db)));
                double Score = c.getDouble(c.getColumnIndex(mContext.getString(R.string.SCOREdb)));

                Ranking ranking = new Ranking(Id, Score);
                listRanking.add(ranking);
            } while (c.moveToNext());
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        db.close();
        return listRanking;

    }



}
zzxbx
  • 135
  • 5
  • 14