SQLite 查询不断崩溃
我有一个查询,它根据我的应用程序中表单中传递的一些参数返回特定记录。这就是我遇到问题的地方:
String[] args={"act_email,act_password"};
Cursor cursor= db.query(DB_TABLE_NAME,new String[]{"email","password"},"email"+" like" + " %?%"+"AND"+" password "+" like" + " %?%",new String[]{"act_email,act_password"},null,null, null);
这是我在网上找到的一些代码:
Cursor cursor= db.rawQuery("SELECT email,password FROM users WHERE email LIKE '%' || ? || '%' and password LIKE '%' || ? || '%'",args);
该代码不会导致任何问题,但它确实需要一些优化,主要是因为它总是返回一个空值。关于如何解决这个问题有什么建议吗?
I have this query that returns a particular record based on some parameters passed within a form in my application. This is where I get the problem:
String[] args={"act_email,act_password"};
Cursor cursor= db.query(DB_TABLE_NAME,new String[]{"email","password"},"email"+" like" + " %?%"+"AND"+" password "+" like" + " %?%",new String[]{"act_email,act_password"},null,null, null);
Here is some code that I found online:
Cursor cursor= db.rawQuery("SELECT email,password FROM users WHERE email LIKE '%' || ? || '%' and password LIKE '%' || ? || '%'",args);
This code does not cause any problems but it does need some optimisation mainly because it always returns an empty value. Any suggestions on how to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么要串联那么多?
您的第一个 where 条件和
AND
之间缺少空格。使用
“电子邮件如'%?%'和密码如'%?%'”
作为第三个参数。另外:您确定要接受作为数据库中存储的密码的一部分的每个密码输入吗?在这种情况下,“e”将是有效的输入,即使密码是“thejdsfsdaf2313!!!”。
Why do you concatenate that much?
You have a missing space between your first where-condition and the
AND
.Use
"email like '%?%' AND password like '%?%'"
as third argument.Also: Are you sure you want to accept every password-input which is part of the password stored in the database? In this case "e" would be a valid input, even if the password is "thejdsfsdaf2313!!!".
您的参数不正确,因为它缺少两个元素之间的引号...大括号之间必须有 2 个单独的字符串。这应该是:
编辑(开始)
其次,您在查询选择字符串中缺少'
文本限定符,对于文本字段,这些限定符应采用以下形式:< /strike>field='?'SQLite 似乎自己处理引号,因此您需要做的就是以以下形式提供 where 子句:
然后在 args 数组中提供参数。看来 Android 的 OpenDatabaseHelper 类会根据类型自动处理文本限定符。
编辑(结束)
请注意,以这种方式在 Android 上使用 SQLite 的参数化查询不喜欢查询字符串中的
%
通配符。要使用通配符,请将它们放在 arg 值中,这将在执行过程中被替换。所以你的参数变成:或者如果你想在这里使用变量:
所以完整的代码应该看起来像这样:
虽然我应该指出在电子邮件和密码的情况下你不应该使用
like
。将 where 子句更改为:
Your args are incorrect as it is missing the quote marks between the two elements... you must have 2 separate strings between the curly brackets. This should read:
Edit (Start)
Secondly you are missing the'
text qualifiers in your query selection string, for text fields these should be of the form:field='?'SQLite appears to handle the handle the quotes on its own, so all you need to do provide the where clause in the form:
and then supply the parameters in an args array. It appears that Androids OpenDatabaseHelper class handles the text qualifiers automatically depending on type.
Edit (End)
Please note that parameterized queries in using SQLite on Android in this way do not like
%
wilcards in the query string. To use wildcards place them in the arg value instead and this will be substituted in during execution. So your args become:OR if you want to use variables here:
So the full code should look something like this:
Although I shoulld point out in the case of email and password you should not use
like
.Change the where clause to:
你有这样尝试过吗?我已经举了一个例子。
Have you tried like this? I have given an example.