Code Poet
Code Poet

Reputation: 11437

sqlite: SQLiteDase.query not working as expected

I have a table like so:

1|EC7599A156DB1913AD9DE9A3A9540624B05683EC|Bar|World's Greatest|http://www.foo.com
2|EC7599A156DB1913AD9DE9A3A9540624B05683EC|Bar|World's Greatest|http://www.foo.com
3|EC7599A156DB1913AD9DE9A3A9540624B05683EC|Bar|World's Greatest|http://www.foo.com

The schema for the table is:

CREATE TABLE IRChannels (
    ChannelId INTEGER PRIMARY KEY, 
    ChannelHash BLOB NOT NULL, 
    ChannelTitle TEXT NOT NULL, 
    ChannelDesc TEXT, 
    ChannelLink TEXT);

I try to run a SELECT statement using the following code:

if (mReadDatabase != null) {
     String[]   columns = new String[]{COL_CHANNEL_ID};
     String     selection = COL_CHANNEL_HASH + "='" + hash + "'";
     try {
         Cursor cursor = mReadDatabase.query(
             TABLE_CHANNELS_NAME,      // Table Name 
             columns,                  // Channel ID
             selection,                // WHERE ChannelHash
             null,                     // = hash
             null,                     // Don't  Group
             null,                     // Don't filter by Groups
             null,                     // Use default Sort Order
             String.valueOf(1));       // LIMIT 1

         if (cursor.moveToFirst()) {
             return cursor;
         } else 
             return null;
    }

Problem is I never see any data in the returned cursor. If I try the following command in the sqlite3 shell, I'm able to get the data:

select channelid 
from irchannels 
where channelhash='EC7599A156DB1913AD9DE9A3A9540624B05683EC' 
limit 1

What could I be missing?

UPDATE: I tried changing my code to using raw query like so, still no difference:

if (mReadDatabase != null) {
    String        selection = COL_CHANNEL_HASH + "='" + hash + "'";
    final String  rawQuery = "SELECT " + COL_CHANNEL_ID +  
                " FROM " + TABLE_CHANNELS_NAME + " " + 
                " WHERE " + selection + 
                " LIMIT 1;";
        Cursor cursor = mReadDatabase.rawQuery(rawQuery, null);
}

Upvotes: 1

Views: 543

Answers (2)

Yury
Yury

Reputation: 20936

Somewhere I've read that android requires for id field special name. You can get it as in the example:

public static final String COL_CHANNEL_ID = BaseColumns._ID;

And the schema should be the following in your case:

CREATE TABLE IRChannels (
    _id INTEGER PRIMARY KEY, 
    ChannelHash BLOB NOT NULL, 
    ChannelTitle TEXT NOT NULL, 
    ChannelDesc TEXT, 
    ChannelLink TEXT);

Because BaseColumns._ID usually equal to "_id"

Upvotes: 0

Lalit Poptani
Lalit Poptani

Reputation: 67296

Try this,

String[]   columns = new String[]{COL_CHANNEL_ID};

Cursor cursor = mReadDatabase.query(
             TABLE_CHANNELS_NAME,      // Table Name 
             columns,                  // Channel ID
             "column_name=?",                // WHERE ChannelHash
             new String[]{hash},                     // = hash
             null,                     // Don't  Group
             null,                     // Don't filter by Groups
             null,                     // Use default Sort Order
             String.valueOf(1));       // LIMIT 1

Upvotes: 1

Related Questions