2012-04-15 8 views
0

intと文字列を使用してクエリを実行する方法はありません。私は2つのintを持つサンプルを見つけました。私は2つの文字列を持つサンプルを見つけました。私はさまざまな組み合わせを試みたが、何も動作していないようだ。以下は、私が試したことと働いていないことの2つの例です。私は間違って何をしています。私はそこにinpspection_idとitem_nameの両方を使用しており、どちらも動作します。intと文字列を使用してクエリを試してみよう

は1をお試しください:

public int getId(int inspection_id ,String item_name)throws Exception 
{ 
    Cursor c = db.query(DB_TABLE, new String[] {KEY_INSPECTION_ID, KEY_ITEM_NAME}, 
      "inspection_id=" + inspection_id +"item_name='"+ item_name + "'", null, null, null, null); 
    int id=c.getColumnIndex(KEY_ROWID); 
    c.moveToFirst(); 
    String result=c.getString(id); 
    int primId=Integer.parseInt(result); 
    c.close(); 
    return primId; 

} 

は2を試してみてください。

public int getId(int inspection_id ,String item_name)throws Exception 
{ 
    Cursor c = db.query(DB_TABLE, new String[] {KEY_INSPECTION_ID, KEY_ITEM_NAME}, 
      "inspection_id=" + inspection_id +"AND KEY_ITEM_NAME = ?", new String[]{item_name}, null, null, null); 
    int id=c.getColumnIndex(KEY_ROWID); 
    c.moveToFirst(); 
    String result=c.getString(id); 
    int primId=Integer.parseInt(result); 
    c.close(); 
    return primId; 

} 

3を試してみてください:

public int getId(int inspection_id ,String item_name)throws Exception 
{ 
    String s_id= Integer.toString(inspection_id); 
    Cursor c = db.query(DB_TABLE, new String[] {KEY_INSPECTION_ID, KEY_ITEM_NAME}, 
      "KEY_INSPECTION_ID = ? AND KEY_ITEM_NAME = ?", 
      new String[] { s_id, item_name }, null, null, null); 
    int id=c.getColumnIndex(KEY_ROWID); 
    c.moveToFirst(); 
    String result=c.getString(id); 
    int primId=Integer.parseInt(result); 
    c.close(); 
    return primId; 

} 

答えて

2

このようにそれをしようと、int型のもののために、単一引用符で。

"inspection_id='" + inspection_id +"'item_name='"+ item_name + "'" 
+0

は、私はそれを試してみましたが、それが何か他のものであることが判明 – Aaron

+0

おかげで動作しませんでしたが、私はそれを変更したときにコメントが働いていました – Aaron