views:

92

answers:

3

friends, I need help in sqlite query for check whether the data is already exist in table, or to check table is empty or not,let's give queries for it.

Thanks in advance.

+1  A: 
SELECT COUNT(*) FROM `tableName`;

if the result is 0 the table is empty ;)

zolex
+1  A: 

Also please kindly read about 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;
}
Pentium10
+1  A: 

REFINED QUERY

SELECT COUNT(*) FROM TABLE WHERE ROWNUM=1

Suresh S