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

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

     public boolean isTableExists(String tableName) {
        boolean isExist = false;
        Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + tableName + "'", null);
        if (cursor != null) {
            if (cursor.getCount() > 0) {
                isExist = true;
            }
            cursor.close();
        }
        return isExist;
    }
    

提交回复
热议问题