gpt4 book ai didi

android - 尝试选择时SQLite Exception没有这样的列

转载 作者:IT老高 更新时间:2023-10-28 22:04:46 25 4
gpt4 key购买 nike

我有以下数据库助手类:

public int studentExists(String studid) {
Cursor dataCount = mDb.rawQuery("select count(*) from usertable where " + KEY_STUDID + "=" + studid, null);
dataCount.moveToFirst();
int count = dataCount.getInt(0);
dataCount.close();
return count;
}

我在我的应用中使用它来查看之前是否输入了学生 ID。

当学生 ID 为整数 (346742) 时,这可以正常工作,但每当我尝试添加字母数字 ID (PB3874) 时,它都会强制关闭应用程序。

错误:

06-13 18:22:20.554: ERROR/AndroidRuntime(8088): android.database.sqlite.SQLiteException: no such column: pb3874: , while compiling: select count(*) from usertable where studid=pb3874

我认为不是数据类型问题(因为我使用的是文本类型):

    private static final String DATABASE_CREATE =
"create table usertable (_id integer primary key autoincrement, "
+ "studid text not null);";

但我很困惑为什么错误说 no such column: pb3874 因为我试图简单地从 studid 列中选择该值。以及为什么这对于任何 int 值都非常有效。有人有解决问题的建议吗?

最佳答案

这里发生的是 SQLite 认为 'pb3874' 实际上是列名,而不是字符串/文本文字。

要指定它是一个文本文字,您需要确保您的文本值包含在适当的单引号中:

为防止 SQL 注入(inject)攻击,每当您从用户那里获取输入时,请使用参数化查询:

("select count(*) from usertable where " + KEY_STUDID + "=?", studid);

没有参数化(在接受用户输入时非常不鼓励):

("select count(*) from usertable where " + KEY_STUDID + "='" + studid + "'", null);  

你的数值没有产生这个的原因:SQLite 为你转换了这些数字文字。

关于android - 尝试选择时SQLite Exception没有这样的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6337296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com