开发者

This SQLite query is returning a cursor with a count of 0... I know it should have at least 1. Can someone have a look? (Real quick)

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_WORK开发者_开发问答PHONE, 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.


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.


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);


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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜