Sqlite检查表是否为空[重复]

时间:2022-09-15 16:29:45

This question already has an answer here:

这个问题已经有了答案:

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.

我将使用它,但它是requre API 11。

7 个解决方案

#1


31  

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

您可以从表中执行select count(*)并检查count> 0,然后让else填充它。

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

#2


6  

Do a 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;

#3


5  

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;
    }

#4


2  

Here is a better option:

这里有一个更好的选择:

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

#5


2  

 public boolean isEmpty(String TableName){

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

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

#6


1  

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;
}
}

#7


0  

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,如果表为空。回答被标记为正确-使用额外的检查。

#1


31  

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

您可以从表中执行select count(*)并检查count> 0,然后让else填充它。

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

#2


6  

Do a 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;

#3


5  

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;
    }

#4


2  

Here is a better option:

这里有一个更好的选择:

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

#5


2  

 public boolean isEmpty(String TableName){

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

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

#6


1  

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;
}
}

#7


0  

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,如果表为空。回答被标记为正确-使用额外的检查。