检查记录是否存在于SQLite数据库表中

时间:2014-04-16 11:47:45

标签: android sqlite android-sqlite

如何检查如果记录存在ImageName或不存在数据库表,我使用下面的代码检查?

// Check for Record using ImageName
    public boolean Exists(String strImageName) {
           SQLiteDatabase db;
           db = this.getReadableDatabase(); // Read Data
           Cursor cursor = db.rawQuery("select 1 from churchTable where ImageName=%s", 
                new String[] { strImageName });
           boolean exists = (cursor.getCount() > 0);
           cursor.close();
           return exists;
    }

1 个答案:

答案 0 :(得分:2)

只需将%s替换为?,以便在那里使用strImageName绑定参数:

Cursor cursor = db.rawQuery("select 1 from churchTable where ImageName=?", 
            new String[] { strImageName });