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

前端 未结 12 1921
栀梦
栀梦 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;
    
    0 讨论(0)
  • 2020-11-27 13:07

    no such table exists: error is coming because once you create database with one table after that whenever you create table in same database it gives this error.

    To solve this error you must have to create new database and inside the onCreate() method you can create multiple table in same database.

    0 讨论(0)
  • 2020-11-27 13:13

    ..... Toast t = Toast.makeText(context, "try... " , Toast.LENGTH_SHORT); t.show();

        Cursor callInitCheck = db.rawQuery("select count(*) from call", null);
    
        Toast t2a = Toast.makeText(context, "count rows " + callInitCheck.getCount() , Toast.LENGTH_SHORT);
        t2a.show();
    
        callInitCheck.moveToNext();
        if( Integer.parseInt( callInitCheck.getString(0)) == 0) // if no rows then do
        {
            // if empty then insert into call
    

    .....

    0 讨论(0)
  • 2020-11-27 13:15

    Yep, turns out the theory in my edit was right: the problem that was causing the onCreate method not to run, was the fact that SQLiteOpenHelper objects should refer to databases, and not have a separate one for each table. Packing both tables into one SQLiteOpenHelper solved the problem.

    0 讨论(0)
  • 2020-11-27 13:17
     // @param db, readable database from SQLiteOpenHelper
    
     public boolean doesTableExist(SQLiteDatabase db, String tableName) {
            Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + tableName + "'", null);
    
        if (cursor != null) {
            if (cursor.getCount() > 0) {
                cursor.close();
                return true;
            }
            cursor.close();
        }
        return false;
    }
    
    • sqlite maintains sqlite_master table containing information of all tables and indexes in database.
    • So here we are simply running SELECT command on it, we'll get cursor having count 1 if table exists.
    0 讨论(0)
  • 2020-11-27 13:19

    I know nothing about the Android SQLite API, but if you're able to talk to it in SQL directly, you can do this:

    create table if not exists mytable (col1 type, col2 type);
    

    Which will ensure that the table is always created and not throw any errors if it already existed.

    0 讨论(0)
提交回复
热议问题