如果行存在,如何检查sqlite db?我在实现sqlite db时的风格是这样的
public class EmployeeDBController extends SQLiteOpenHelper {
public EmployeeDBController(Context applicationcontext) {
super(applicationcontext, "registeruser.db", null, 1);
}
public SQLiteDatabase db;
//Creates Table
@Override
public void onCreate(SQLiteDatabase database) {
String query;
query = "CREATE TABLE register ( userId INTEGER PRIMARY KEY, Name text, Age text, Gender text," +
"HomeAddress text, Password text, QRID text, udpateStatus TEXT)";
database.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
String query;
query = "DROP TABLE IF EXISTS register";
database.execSQL(query);
onCreate(database);
}
}
例如:我想检查sqlite db中是否存在“john”的“Name”。
答案 0 :(得分:1)
有一个helper function可以避免不得不用光标捣乱:
public boolean rowExists(String table, String column, String value) {
long count = DatabaseUtils.queryNumEntries(db,
table, column+" = ?", new String[]{ value });
return count > 0;
}
答案 1 :(得分:0)
使用此方法!
public boolean isRowExist(/*YOUR_INPUTS*/){
try {
db = mDBHelper.getReadableDatabase();
String selectQuery = YOUR_SEARCH_QUERY_STATMENT
//You should use YOUR_INPUTS to create a selectQuery that can select the row/rows for you
Cursor c = db.rawQuery(selectQuery, null);
if(c.getCount()>=1){
c.close();
return true;
}
c.close();
return false;
} catch (Exception e) {
return false;
}
}