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

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

    Although there are already a lot of good answers to this question, I came up with another solution that I think is more simple. Surround your query with a try block and the following catch:

    catch (SQLiteException e){
        if (e.getMessage().contains("no such table")){
                Log.e(TAG, "Creating table " + TABLE_NAME + "because it doesn't exist!" );
                // create table
                // re-run query, etc.
        }
    }
    

    It worked for me!

提交回复
热议问题