2013-08-31 52 views
0

我是新來的android。簡單的select查詢參數傳遞問題

我只是想對數據庫中的特定用戶計數表中的行數(uid是db中的列)。

爲此我做了以下功能:

public int getCount(int uid) 
    { 
     String query = "select count(*) from "+TABLE_MESSAGES+ " WHERE uid=?"; 

     Cursor c = ourDB.rawQuery(query,uid); 

    } 

但它給我的錯誤:

The method rawQuery(String, String[]) in the type SQLiteDatabase is not applicable for the arguments (String, int)

也想知道我怎麼能夠從這一回算?

請幫幫我。

回答

2

,你應該用這樣的方式:

public int getCount(int uid) 
{ 
    String query = "select count(*) from "+TABLE_MESSAGES+ " WHERE uid="+uid+""; 

    Cursor c = ourDB.rawQuery(query,null); 

    if(c.getCount()>0) 
    c.moveToFirst(); 

    do{ 

    int id = c.getInt(0); 

    }while(c.moveToNext()); 

} 
+0

如果正在查詢傳遞'uid'直接本身爲什麼你需要'?',這是錯誤的.... – CRUSADER

1

改變您的查詢:

String query = "select count(*) from "+TABLE_MESSAGES+ " WHERE uid="+uid; 

Cursor c = ourDB.rawQuery(query); 
1

The method rawQuery(String, String[]) in the type SQLiteDatabase is not applicable for the arguments (String, int)

顯然指出你INT傳遞,而不是預期的String []

Cursor c = ourDB.rawQuery(query,new String[]{ uid }); 
2

試試這個方法

public int getCount(int uid){ 
     try{ 
      String query = "select count(*)'count' from "+TABLE_MESSAGES+ " WHERE uid="+uid+""; 

      Cursor c = ourDB.rawQuery(query,null); 

      if (c.moveToFirst()) { 

       return Integer.parseInt(cursor.getString(0)); 

      } 
     }catch (Throwable e) { 
      e.printStackTrace(); 
     } 
     return 0; 
    } 
1

使用查詢作爲:

"SELECT * FROM <DB_NAME> WHERE uid="+uid 

,然後使用getCount將()方法上的光標等 :

Cursor csr = db.rawQuery("above query string here"); 
int count = csr.getCount();