检查sqlite

时间:2017-12-25 02:17:09

标签: android sqlite

我有一个带有sqllite数据库的Android应用程序。该数据库有各种表。到目前为止,我有两个表,我有一个错误:当我创建表A时,当我运行表B的代码时,我的应用程序崩溃;当我先创建表B然后再创建A时,应用程序也会崩溃。我必须清除我的应用程序缓存,以便我可以“更改”访问权限,因此我永远无法在同一个应用程序“运行”中访问它们。我认为这是因为我在添加新表时重新创建数据库。为了避免这种情况,我试图检查数据库是否已经存在,如果没有重新创建,但我在构造函数中检查它并且android不允许它。

日志:

  

12-25 00:30:28.024 32031-32031 / com.support.android.iplfit E / AndroidRuntime:致命异常:主要                                                                               处理:com.support.android.iplfit,PID:32031                                                                               android.database.sqlite.SQLiteException:没有这样的表:Dica(代码1):,编译时:DELETE FROM Dica                                                                                   在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)                                                                                   在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)                                                                                   在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)                                                                                   在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                                                                                   在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)                                                                                   在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)                                                                                   在android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1499)                                                                                   在com.support.android.iplfit.BDHelpers.DicaBDHelper.removerAllDicasBD(DicaBDHelper.java:73)                                                                                   在com.support.android.iplfit.Singletons.SingletonDicas.adicionarDicasBD(SingletonDicas.java:65)                                                                                   在com.support.android.iplfit.Singletons.SingletonDicas $ 1.onResponse(SingletonDicas.java:83)                                                                                   at com.support.android.iplfit.Singletons.SingletonDicas $ 1.onResponse(SingletonDicas.java:77)                                                                                   在com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)                                                                                   在com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)                                                                                   在android.os.Handler.handleCallback(Handler.java:751)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                   在android.os.Looper.loop(Looper.java:154)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:6077)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

我创建一个表的其中一个类:

public class DicaBDHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "db_iplfit";
    private static final int DB_VERSION = 1;
    private static final String TABLE_NAME = "Dica";

    private static final String ID_DICA = "id";
    private static final String CHANNEL_DICA = "channel";
    private static final String TITULO_DICA = "titulo";
    private static final String CONTEUDO_DICA = "conteudo";
    private final SQLiteDatabase database;

    public DicaBDHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);

            this.database = getWritableDatabase();
    }

    private static boolean haveDB(Context context, String dbName) {
        File dbFile = context.getDatabasePath(dbName);
        return dbFile.exists();
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        String createDicaTable = "CREATE TABLE " + TABLE_NAME + "( " +
                ID_DICA + " INTEGER UNSIGNED PRIMARY KEY," +
                CHANNEL_DICA + " TEXT NOT NULL," +
                TITULO_DICA + " TEXT NOT NULL," +
                CONTEUDO_DICA + " TEXT NOT NULL" + ")";
        database.execSQL(createDicaTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int i, int j) {
        String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
        database.execSQL(sql);
        //this.onCreate(database);
    }
}

2 个答案:

答案 0 :(得分:1)

选项1

使用onCreate创建多个表: -

@Override
public void onCreate(SQLiteDatabase database) {
    String createDicaTable = "CREATE TABLE " + TABLE_NAME + "( " +
            ID_DICA + " INTEGER UNSIGNED PRIMARY KEY," +
            CHANNEL_DICA + " TEXT NOT NULL," +
            TITULO_DICA + " TEXT NOT NULL," +
            CONTEUDO_DICA + " TEXT NOT NULL" + ")";
    database.execSQL(createDicaTable);

    // Next table
    String createanothertable = "CREATE TABLE anothertable (column TEXT)";
    database.execSQK(createanothertable);
    // etc
}

请注意! onCreate仅在数据库不存在时自动运行

选项2

创建后续(缺失)表(假设与其他其他表无关),而不必删除现有表。

按需运行或每次运行App时(因此代码IF NOT EXISTS)。

public void addMissingTables() {
    String createanothertable = "CREATE TABLE IF NOT EXISTS anothertable (column TEXT)";
    database.execSQL(createanothertable);
    // etc
}
  • 请注意!假设方法被添加到Databasehelper(so db)

  • 请注意!在获得DatabaseHelper的实例后,不应该使用问题,因为this.database = getWritableDatabase();将强制DatabaseHelper创建数据库(如果它不存在)。

  • 您还可以为IF NOT EXISTS中的表编码onCreate,并以类似方式调用它。

答案 1 :(得分:0)

private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH(DB_FULL_PATH is the path to your database file), null,
                SQLiteDatabase.OPEN_READONLY);
        checkDB.close();
    } catch (SQLiteException e) {
        // database doesn't exist yet.
    }
    return checkDB != null;
}