从光标获取表信息(存在/不存在)

时间:2013-01-02 15:15:07

标签: android

在我的活动中,无法使用

检查该表是否存在
 Cursor cursor = marksdb.rawQuery("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='"+classt+"'", null);

如果表存在,则为0,否则为0 因为我需要使用

检查光标中的值
if(cursor.getCount()==1){
        // get values from cursor here
         callclasstb();
        }
     else{
         tv.setVisibility(View.VISIBLE);
        subjectet.setEnabled(false);
        markset.setEnabled(false);
        markssp.setEnabled(false);
     }

但是在所有情况下我都得到值1因为getcout()返回值1 r 0和

callclasstb();
如果{....}使其执行

不会执行我需要写入的条件

1 个答案:

答案 0 :(得分:1)

在您的情况下,

cursor.getCount()将始终返回1(结果集中的“行”数)。你需要找出通过光标返回的内容。

     Cursor cursor = marksdb.rawQuery(...);
     cursor.moveToFirst(); // first "row"
     int nTableExists = cursor.getInt(0);
     cursor.close();

     if (nTableExists) { // != 0
        ...do something...
     }