Android Sqlite 检查表是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22630307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:16:32  来源:igfitidea点击:

Sqlite Check if Table is Empty

androiddatabasesqlite

提问by mehmet

Well, I have a databse and it has lots of table. but generally tables are empty. I want check if a database table is empty. IF table is empty, program will fill it.

好吧,我有一个数据库,它有很多表。但通常表是空的。我想检查数据库表是否为空。如果表为空,程序将填充它。

public static long queryNumEntries (SQLiteDatabase db, String table)

I will use it but it requre API 11.

我会使用它,但它需要 API 11。

回答by Waqar Ahmed

you can execute select count(*) from tableand check if count> 0then leave else populate it.

您可以执行select count(*) from table并检查是否count> 0然后留下其他填充它。

like

喜欢

 SQLiteDatabase db = table.getWritableDatabase();
String count = "SELECT count(*) FROM table";
Cursor mcursor = db.rawQuery(count, null);
mcursor.moveToFirst();
int icount = mcursor.getInt(0);
if(icount>0)
//leave 
else
//populate table

回答by Luca Sepe

Do a SELECT COUNT:

做一个SELECT COUNT

boolean empty = true
Cursor cur = db.rawQuery("SELECT COUNT(*) FROM YOURTABLE", null);
if (cur != null && cur.moveToFirst()) {
    empty = (cur.getInt (0) == 0);
}
cur.close();

return empty;

回答by Dwivedi Ji

Optimal Solutions

最佳解决方案

public boolean  isMasterEmpty() {

    boolean flag;
    String quString = "select exists(select 1 from " + TABLE_MASTERS  + ");";

    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery(quString, null);
    cursor.moveToFirst();
    int count= cursor.getInt(0);
    if (count ==1) {
        flag =  false;
    } else {
        flag = true;
    } 
    cursor.close();
    db.close();

    return flag;
    }

回答by Mahesh Uligade

 public boolean isEmpty(String TableName){

    SQLiteDatabase database = this.getReadableDatabase();
    int NoOfRows = (int) DatabaseUtils.queryNumEntries(database,TableName);

    if (NoOfRows == 0){
        return true;
    }else {
        return false;
    }
}

回答by Juan Pablo Arango

Here is a better option:

这是一个更好的选择:

public boolean validateIfTableHasData(SQLiteDatabase myDatabase,String tableName){
    Cursor c = myDatabase.rawQuery("SELECT * FROM " + tableName,null);
    return c.moveToFirst();
}

回答by My God

This is how you can do it-

这就是你可以做到的-

if(checkTable("TABLE"))
{
  //table exists fill data.
}

Method to check table-

查表方法——

public static boolean checkTable(String table) {
Cursor cur2 = dbAdapter.rawQuery("select name from sqlite_master where name='"
        + table + "'", null);

if (cur2.getCount() != 0) {
    if (!cur2.isClosed())
        cur2.close();
    return true;
} else {
    if (!cur2.isClosed())
        cur2.close();
    return false;
}
}

回答by Денис Климков

I think, this solution is better:

我认为,这个解决方案更好:

    boolean flag;

    DatabaseHelper databaseHelper = new DatabaseHelper(getApplicationContext(), DatabaseHelper.DATABASE_NAME, null, DatabaseHelper.DATABASE_VERSION);
    try {
        sqLiteDatabase = databaseHelper.getWritableDatabase();
    } catch (SQLException ex) {
        sqLiteDatabase = databaseHelper.getReadableDatabase();
    }
    String count = "SELECT * FROM table";
    Cursor cursor = sqLiteDatabase.rawQuery(count, null);
    if (cursor.moveToFirst()){
        flag = false;
    } else {
        flag = true;
    }
    cursor.close();
    sqLiteDatabase.close();

    return flag;

moveToFirst()check table and return true, if table is empty. Answer that is marked correct - uses extra check.

moveToFirst()检查表并返回true,如果表为空。标记为正确的答案 - 使用额外检查。