我如何在SQLite中传递int作为where子句的参数?

时间:2014-04-10 08:51:53

标签: java android sql sqlite

我正在使用intent.getExtra()...来获取int值,我想在where子句中使用它。所以如何做,因为它只需要字符串作为参数。

String keyvalue;    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mobile);
        keyvalue = getIntent().getExtras().getInt("key")

 public ArrayList<HashMap<String,String>> getAllMobiles() {
        ArrayList <HashMap<String,String>> wordlist;
        wordlist= new ArrayList <HashMap<String, String>>();
        String Query = "SELECT * FROM subcategories WHERE _id =?";  

        SQLiteDatabase database = sql.getWritableDatabase();

        Cursor cursor = database.rawQuery(Query, new String[] {keyvalue});
        if (cursor.moveToFirst()) {
            do {
            HashMap<String,String> map = new HashMap<String, String>();

            map.put(Sqlite.SUBCATEGORY_NAME, cursor.getString(2));          
            wordlist.add(map);
            } while (cursor.moveToNext());

2 个答案:

答案 0 :(得分:1)

要将int转换为字符串,请使用例如

String.valueOf(keyvalue)

所以查询变为

Cursor cursor = database.rawQuery(Query, new String[] { String.valueOf(keyvalue) });

答案 1 :(得分:0)

只需将整数值解析为字符串并将其添加到sql Query

即可