How does one check if a table exists in an Android SQLite database?

前端 未结 12 2090
栀梦
栀梦 2020-11-27 12:37

I have an android app that needs to check if there\'s already a record in the database, and if not, process some things and eventually insert it, and simply read the data fr

12条回答
  •  渐次进展
    2020-11-27 13:07

    This is what I did:

    /* open database, if doesn't exist, create it */
    SQLiteDatabase mDatabase = openOrCreateDatabase("exampleDb.db", SQLiteDatabase.CREATE_IF_NECESSARY,null);
    
    Cursor c = null;
    boolean tableExists = false;
    /* get cursor on it */
    try
    {
        c = mDatabase.query("tbl_example", null,
            null, null, null, null, null);
            tableExists = true;
    }
    catch (Exception e) {
        /* fail */
        Log.d(TAG, tblNameIn+" doesn't exist :(((");
    }
    
    return tableExists;
    

提交回复
热议问题