2013-08-31 1 views
0

나는 android에서 새거야.간단한 선택 쿼리 인수 전달 문제

DB의 특정 사용자에 대해 테이블의 행 수를 가져 오려고했습니다 (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)

또한 내가이에서 수를 반환 할 수있는 방법을 알고 싶어를이를 위해

i는 다음과 같은 기능을했다?

도와주세요. 이 방법으로 사용해야합니다

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

Cursor c = ourDB.rawQuery(query); 

답변

2

:에

+0

당신이 쿼리에 직접'uid' 통과하는 경우 왜'?'가 필요한지, 틀린 것은 .... – CRUSADER

1

변화 쿼리

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()); 

} 
1

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

은 분명히 당신이 대신 INT 전달하는 상태 문자열 []

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

시도해주세요. 같은 커서 에 getCount() 메소드를 사용하여 다음

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

과 :

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

같이 쿼리를 사용

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