这个 SQLite 查询返回一个计数为 0 的游标...我知道它应该至少有 1。有人可以看一下吗? (真快)

发布于 2024-10-20 09:12:01 字数 538 浏览 3 评论 0原文

public Cursor fetchIsOverride(String username) {
        int override = 1;
        Cursor cursor = mDb.query(true, OVERRIDE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_ISOVERRIDE, KEY_FIRSTNAME, KEY_LASTNAME, KEY_EMAIL, KEY_HOMEPHONE, KEY_WORKPHONE, KEY_PICTURE}, KEY_USERNAME + "= '" + username +"' " + "AND " + KEY_ISOVERRIDE + "=" + override, null, null, null, null, null);
        return cursor;
    }

我在表中有一行 KEY_ISOVERRIDE 值为 1,并且带有我正在查询的用户名。这是肯定的。谁能看出这个语法有什么问题吗?我确定它在 WHERE 子句中,但据我所知,它没问题。谢谢。

public Cursor fetchIsOverride(String username) {
        int override = 1;
        Cursor cursor = mDb.query(true, OVERRIDE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_ISOVERRIDE, KEY_FIRSTNAME, KEY_LASTNAME, KEY_EMAIL, KEY_HOMEPHONE, KEY_WORKPHONE, KEY_PICTURE}, KEY_USERNAME + "= '" + username +"' " + "AND " + KEY_ISOVERRIDE + "=" + override, null, null, null, null, null);
        return cursor;
    }

I have a row on the table with a KEY_ISOVERRIDE value of 1, and with the username I am querying. This is for certain. Can anyone see anything wrong with this syntax? I'm sure its in the WHERE clause, but as far as I can tell it's OK. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

っ左 2024-10-27 09:12:01

如果您的用户名覆盖字符串包含任何标点符号、空格或其他异常字符,则可能需要对其进行转义。最好使用参数绑定 API,而不是将字符串直接插入 SQL 语句中。

If your username or override string has any punctuation, spaces, or other unusual characters, it may need to be escaped. It's better to use the parameter binding apis instead of interpolating strings directly into the SQL statements.

我做我的改变 2024-10-27 09:12:01

尝试在查询的条件部分中使用 double == 而不是 single ==,如下所示

Cursor cursor = mDb.query(true, OVERRIDE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_ISOVERRIDE, KEY_FIRSTNAME, KEY_LASTNAME, KEY_EMAIL, KEY_HOMEPHONE, KEY_WORKPHONE, KEY_PICTURE}, KEY_USERNAME + "== '" + username +"' " + "AND " + KEY_ISOVERRIDE + "==" + override, null, null, null, null, null);

try to use double == instead of single in the condition section of your query like this

Cursor cursor = mDb.query(true, OVERRIDE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_ISOVERRIDE, KEY_FIRSTNAME, KEY_LASTNAME, KEY_EMAIL, KEY_HOMEPHONE, KEY_WORKPHONE, KEY_PICTURE}, KEY_USERNAME + "== '" + username +"' " + "AND " + KEY_ISOVERRIDE + "==" + override, null, null, null, null, null);
三生殊途 2024-10-27 09:12:01

我是个白痴,同时包括 moveToFirst();还有一个 while 循环:

while(moveToNext()) {
    //stuff
}

抱歉给您带来麻烦。但也许其他人也会以这种方式发现他们的错误。

I was being an idiot and including both moveToFirst(); AND a while loop like:

while(moveToNext()) {
    //stuff
}

Sorry for the trouble. But maybe someone else will catch their mistake this way too.

能否归途做我良人 2024-10-27 09:12:01

Cursor c=db.query("tbl_TEMP", new String[]{"sitename"+" as name",
"网站" +" 作为网站","类别" +" 作为类别" },
//"站点名称"+"=?", new String[]{"iPhoneAppDeveloper"}, null, null, null);
"sitename"+"=?", new String[] {"从 DB 中查找特定记录的字符串"} , null, null, null);

if (c.moveToFirst())
{
字符串名称、网站、类别;
int nameColumn = c.getColumnIndex("name");
int websiteColumn = c.getColumnIndex("网站");
int CategoryColumn = c.getColumnIndex("类别");

            do
            {
                // Get the field values
                    name = c.getString(nameColumn);
                    TvXmlData.append("\n" +name );
                    website = c.getString(websiteColumn);
                    TvXmlData.append("\n " + website );
                    categoty = c.getString(categoryColumn);
                    TvXmlData.append("\n " + categoty );

            } while (c.moveToNext());

     }
    c.close();
    db.close();

Cursor c=db.query("tbl_TEMP", new String[]{"sitename"+" as name",
"Website" +" as website","category" +" as category" },
//"sitename"+"=?", new String[]{"iPhoneAppDeveloper"}, null, null, null);
"sitename"+"=?", new String[] {"your string to find perticular record frm DB"} , null, null, null);

if (c.moveToFirst())
{
String name,website,categoty;
int nameColumn = c.getColumnIndex("name");
int websiteColumn = c.getColumnIndex("website");
int categoryColumn = c.getColumnIndex("category");

            do
            {
                // Get the field values
                    name = c.getString(nameColumn);
                    TvXmlData.append("\n" +name );
                    website = c.getString(websiteColumn);
                    TvXmlData.append("\n " + website );
                    categoty = c.getString(categoryColumn);
                    TvXmlData.append("\n " + categoty );

            } while (c.moveToNext());

     }
    c.close();
    db.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文