我有一张这样的表:
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
该表的架构是:
CREATE TABLE IRChannels (
ChannelId INTEGER PRIMARY KEY,
ChannelHash BLOB NOT NULL,
ChannelTitle TEXT NOT NULL,
ChannelDesc TEXT,
ChannelLink TEXT);
我尝试使用以下代码运行SELECT
语句:
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;
}
问题是我从未在返回的游标中看到任何数据。如果我在sqlite3 shell中尝试以下命令,我就能获得数据:
select channelid
from irchannels
where channelhash='EC7599A156DB1913AD9DE9A3A9540624B05683EC'
limit 1
我能错过什么?
更新 我尝试将我的代码更改为使用原始查询,但仍然没有区别:
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);
}
答案 0 :(得分:1)
试试这个,
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
答案 1 :(得分:0)
某处我读过android需要id字段的特殊名称。您可以在示例中获取它:
public static final String COL_CHANNEL_ID = BaseColumns._ID;
在您的情况下,架构应该如下:
CREATE TABLE IRChannels (
_id INTEGER PRIMARY KEY,
ChannelHash BLOB NOT NULL,
ChannelTitle TEXT NOT NULL,
ChannelDesc TEXT,
ChannelLink TEXT);
因为BaseColumns._ID通常等于“_id”