如果在Sqlite中它是0或1,我如何检查该行

时间:2017-04-03 13:17:36

标签: android sqlite

我有一行以管理员身份命名 例如,如果数字0正在做某事,反之亦然,我该如何工作?

这是我的经验,但不起作用

con_db connect = new con_db(Main2Activity.this);
SQLiteDatabase read = connect.getReadableDatabase();
Cursor saved = read.rawQuery("select * from show where administrator=0 or administrator=1",null);
if(saved.getCount() == 0)
Toast.makeText(this, "0", Toast.LENGTH_SHORT).show();
if(saved.getCount() == 1)
Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();

1 个答案:

答案 0 :(得分:0)

getCount()返回游标中的行数。 试试这个:

Cursor saved = read.rawQuery("select * from show",null);
if(cursor.getCount() == 0){
    Toast.makeText(this, "Table is empty", Toast.LENGTH_SHORT).show();
    return;
}
while (saved.moveToNext()) {
    int admin = saved.getInt(saved.getColumnIndex("administrator"));
    if(admin == 0){
        Toast.makeText(this, "0", Toast.LENGTH_SHORT).show();
    }else if(admin == 1){
        Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
    }
}
saved.close()