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

前端 未结 12 2186
栀梦
栀梦 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 12:57

    i faced that and deal with it by try catch as simple as that i do what i want in table if it not exist will cause error so catch it by exceptions and create it :)

    SQLiteDatabase db=this.getWritableDatabase();
            try{
                db.execSQL("INSERT INTO o_vacations SELECT * FROM vacations");
                db.execSQL("DELETE FROM vacations");
            }catch (SQLiteException e){
                db.execSQL("create table o_vacations (id integer primary key ,name text ,vacation text,date text,MONTH text)");
                db.execSQL("INSERT INTO o_vacations SELECT * FROM vacations");
                db.execSQL("DELETE FROM vacations");
            }
    
    

提交回复
热议问题