user2141889
user2141889

Reputation: 2305

Querying sqlite in android

I'm creating a database for my game, everything is working until I want to query one item.

I have been trying few different methods and I can't make it work. I just simply don't see the error in my code.

The code is as follows:

   public Item getItem(String icon) {
       String[] columns = {KEY_ID, KEY_TYPE, KEY_ICON, KEY_LEVEL, KEY_ARMOR, KEY_DAMAGE, KEY_BUY, KEY_SELL};
       Cursor cursor = db.query(DB_TABLE, columns, KEY_ICON + "=" + icon,
       null, null, null, null);
      Item item=null;
       if(cursor != null && cursor.moveToFirst()) {
           item= new Item(cursor.getString(TYPE_COLUMN), 
                     cursor.getString(ICON_COLUMN),
                     cursor.getString(LEVEL_COLUMN),
                     cursor.getString(ARMOR_COLUMN),
                     cursor.getString(DAMAGE_COLUMN),
                     cursor.getString(BUY_COLUMN),
                     cursor.getString(SELL_COLUMN)
                           );
       }
       return item;
   }

The error I'm getting is

No such column: fast_boots (code 1): while compiling: SELECT id, type, icon, level, armor, damage, buy, sell from items where icon=fast_boots

When trying to find .getItem("fast_boots"); I do see the fast_boots in my sql database

Upvotes: 1

Views: 47

Answers (2)

Alécio Carvalho
Alécio Carvalho

Reputation: 13647

To make the query work, maybe you should try this :

KEY_ICON + "= '" + icon + "' "

As 'icon' is a string value. Since you're not specifying it, it is probably trying to understand it as being a column in the projection

This is the wrong way of implementing such functionality, though. Do not perform database queries on the getItem() method itself (main thread), it deserves to run in background, so it won't affect the main thread.

Please read about AsyncTask.

Upvotes: 1

AwadKab
AwadKab

Reputation: 3064

Try this

Cursor cursor = db.query(DB_TABLE, columns, KEY_ICON + "='" + icon +"'",
       null, null, null, null);

I added '

Upvotes: 0

Related Questions