Android 中查询时出现 SQLiteException
我在这一行中收到此异常“绑定或列索引超出范围”:
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
我想检查数据库中是否存在给定的 id,但查询总是抛出此异常,我不知道出了什么问题。我尝试过这个,但例外是相同的:
Cursor cursor = db.query(TABLE_NAME, null, ID+" = ?", new String[] { id }, null, null, null);
这是我编程的方法。如果这里有什么问题,请告诉我:
public boolean existsAcc(String id)
{
//Check the parameters
if (id == null)
{
throw new IllegalArgumentException(
"The id parameter cannot be null");
}
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
if (!cursor.moveToFirst())
{
cursor.close();
return false;
}
cursor.close();
return true;
}
I receive this exception "bind or column index out of range" in this line:
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
I want to check if the given id exists in the database, but always the query throw this exception and I don't know what is wrong. I tried with this and the exception it's the same:
Cursor cursor = db.query(TABLE_NAME, null, ID+" = ?", new String[] { id }, null, null, null);
This is the method I've programmed. If there is something wrong here, please, tell me:
public boolean existsAcc(String id)
{
//Check the parameters
if (id == null)
{
throw new IllegalArgumentException(
"The id parameter cannot be null");
}
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, ID, new String[] { id }, null, null, null);
if (!cursor.moveToFirst())
{
cursor.close();
return false;
}
cursor.close();
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道奇拉格为什么删除他的答案,但他是对的。更改
为
即可解决问题。
I don't know why Chirag deleted his answer, but he was rigth. Changing
to
does the trick.