检查数据是否已经存在于表中,或者检查表是否为空
朋友们,我需要帮助在sqlite查询中检查数据是否已经存在于表中,或者检查表是否为空,让我们来查询它。
提前致谢。
SELECT COUNT(*) FROM `tableName`;
如果结果为0,表格为空;)
精炼查询
SELECT COUNT(*)FROM TABLE WHERE ROWNUM = 1
另请请阅读关于DatabaseUtils。
/**
* checks database if a column has a value in the table
*
* @param db
* @param tableName
* @param column
* @param value
* @param rowid to check against and skip if necessary
* @return boolean
*/
public static boolean ExistsWithName(SQLiteDatabase db, String tableName, String column,
String value, Long rowid) {
String sql = String.format("select 1 from %s where %s = '%s'", tableName, column, value);
if (rowid != null) {
sql += " and _id != " + rowid;
}
Cursor c = null;
Boolean ret = false;
try {
c = db.rawQuery(sql, null);
if (c != null) {
if (c.moveToFirst()) {
ret = (c.getCount() > 0);
} else {
ret = false;
}
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if (c != null)
c.close();
}
return ret;
}
链接地址: http://www.djcxy.com/p/19799.html
上一篇: check whether the data is already exist in table, or to check table is empty
下一篇: How does one check if a table exists in an Android SQLite database?