Android 在 Sqlite 中按 Id 选择行?
我想在android sqlite中按id获取特定行并编写了以下代码,但它不返回任何记录。我编写了一个 getAllRecords() 方法,它返回数据库中的所有记录。
谁能解释我犯的错误?
public Bank getBankById(int bankId)
{
Cursor cursor=null;
Bank bnk = null;
cursor = this.db.rawQuery("select * from " + BanksTable.NAME + " where " + BanksTable.COL_ID + "=" + bankId , null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
int id = cursor.getInt(cursor.getColumnIndex(BanksTable.COL_ID));
String name = cursor.getString(cursor.getColumnIndex(BanksTable.COL_NAME));
String url = cursor.getString(cursor.getColumnIndex(BanksTable.COL_IMAGE_URL));
byte[] image = cursor.getBlob(cursor.getColumnIndex(BanksTable.COL_IMAGE));
bnk=new Bank();
bnk.setId(id);
bnk.setImageURL(url);
bnk.setName(name);
bnk.setImageByteArray(image);
}
cursor.close();
}
return bnk;
}
I want to get a specific row by id in android sqlite and have written the following code, but it does not return any records. I have written a getAllRecords()
method which returns all records from database.
Can anyone explain mistake I am making?
public Bank getBankById(int bankId)
{
Cursor cursor=null;
Bank bnk = null;
cursor = this.db.rawQuery("select * from " + BanksTable.NAME + " where " + BanksTable.COL_ID + "=" + bankId , null);
if (cursor != null)
{
if (cursor.moveToFirst())
{
int id = cursor.getInt(cursor.getColumnIndex(BanksTable.COL_ID));
String name = cursor.getString(cursor.getColumnIndex(BanksTable.COL_NAME));
String url = cursor.getString(cursor.getColumnIndex(BanksTable.COL_IMAGE_URL));
byte[] image = cursor.getBlob(cursor.getColumnIndex(BanksTable.COL_IMAGE));
bnk=new Bank();
bnk.setId(id);
bnk.setImageURL(url);
bnk.setName(name);
bnk.setImageByteArray(image);
}
cursor.close();
}
return bnk;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过将参数更改为名称结束了这个问题。
这很奇怪,但我仍然不知道为什么它不起作用。
我得到像 0,1,2,3,4 这样的银行 ID,也许 SQLite 不允许它或内部影响它,但我仍然不确定。
I ended up this problem by changing parameter to name.
Its weird but I still don't know why it was not working.
I was getting bank id like 0,1,2,3,4 maybe it is not allowed in SQLite or internally affecting it but still I am not sure.
这是我从 sqlite db 获取 custom 对象的方法
Here is my approach to get custem object from sqlite db
我通过在创建表时向 id 添加“自动增量”属性解决了这个问题
I solved this issue by adding "autoincrement" property to id, while creating the table