SQLite 查询不断崩溃

发布于 2024-12-01 07:14:46 字数 562 浏览 2 评论 0原文

我有一个查询,它根据我的应用程序中表单中传递的一些参数返回特定记录。这就是我遇到问题的地方:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无需解释 2024-12-08 07:14:46

为什么要串联那么多?

您的第一个 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!!!".

鸠书 2024-12-08 07:14:46

您的参数不正确,因为它缺少两个元素之间的引号...大括号之间必须有 2 个单独的字符串。这应该是:

String[] args= {"act_email","act_password"};

编辑(开始)

其次,您在查询选择字符串中缺少 ' 文本限定符,对于文本字段,这些限定符应采用以下形式:< /strike>

field='?'

SQLite 似乎自己处理引号,因此您需要做的就是以以下形式提供 where 子句:

field=? [ AND field2=? ... AND fieldn=? ]

然后在 args 数组中提供参数。看来 Android 的 OpenDatabaseHelper 类会根据类型自动处理文本限定符。

编辑(结束)

请注意,以这种方式在 Android 上使用 SQLite 的参数化查询不喜欢查询字符串中的 % 通配符。要使用通配符,请将它们放在 arg 值中,这将在执行过程中被替换。所以你的参数变成:

String[] args= {"%act_email%","%act_password%"};

或者如果你想在这里使用变量:

String[] args= {"%" + act_email + "%", "%" + act_password+ "%"};

所以完整的代码应该看起来像这样:

String[] fields = {"email","password"};
String[] args= {"%" + act_email + "%", "%" + act_password+ "%"};

//do not use like for usr & pwd
String where = "email like ? AND password like ?";

Cursor cursor= db.query(DB_TABLE_NAME, fields , where, args,null, null, null);

虽然我应该指出在电子邮件和密码的情况下你不应该使用like

将 where 子句更改为:

String where = "email=? AND password =?";//do not use like for usr & pwd

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:

String[] args= {"act_email","act_password"};

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:

field=? [ AND field2=? ... AND fieldn=? ]

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:

String[] args= {"%act_email%","%act_password%"};

OR if you want to use variables here:

String[] args= {"%" + act_email + "%", "%" + act_password+ "%"};

So the full code should look something like this:

String[] fields = {"email","password"};
String[] args= {"%" + act_email + "%", "%" + act_password+ "%"};

//do not use like for usr & pwd
String where = "email like ? AND password like ?";

Cursor cursor= db.query(DB_TABLE_NAME, fields , where, args,null, null, null);

Although I shoulld point out in the case of email and password you should not use like.

Change the where clause to:

String where = "email=? AND password =?";//do not use like for usr & pwd
江挽川 2024-12-08 07:14:46
String[] args={"act_email,act_password"};
      Cursor cursor= db.query(DB_TABLE_NAME,new String[]{"email","password"},
"email like '%"+args[0]+"%' and password like '%"+args[1]+"%'",args,null,null, null);
String[] args={"act_email,act_password"};
      Cursor cursor= db.query(DB_TABLE_NAME,new String[]{"email","password"},
"email like '%"+args[0]+"%' and password like '%"+args[1]+"%'",args,null,null, null);
云胡 2024-12-08 07:14:46

你有这样尝试过吗?我已经举了一个例子。

  Cursor cursor1=db.rawQuery("SELECT taskid,taskname,taskdescription,tasktime,issync,userid,taskdate FROM task WHERE taskname like '%"+strTaskName+"%'", null);

Have you tried like this? I have given an example.

  Cursor cursor1=db.rawQuery("SELECT taskid,taskname,taskdescription,tasktime,issync,userid,taskdate FROM task WHERE taskname like '%"+strTaskName+"%'", null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文