有没有办法知道数据库是否为空?

时间:2014-07-07 15:42:10

标签: android sqlite

我正在使用SQLiteDatabase,我想在数据库上只创建一行,所以我想使用if语句,我的问题是是否有办法知道数据库是否为空。 谢谢!

1 个答案:

答案 0 :(得分:1)

编辑:

我会更新我的回答,以帮助您更进一步。

如果您想知道表格是否存在以及是否有行

Cursor query = myDB.rawQuery("SELECT count(*) FROM sqlite_master WHERE name = 'table1'", null);
if (query.moveToFirst()) {
    // If that table exists, check if it has any rows
    query = myDB.rawQuery("SELECT count(*) FROM table1", null);
    if (query.moveToFirst()) {
        // Table exists and it has rows. Do something with them here.
    }
}