Unable to fetch an sqlite record - android

I am using the following query to fetch a row with a particular mid.
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, "actionData", "mid" }, "mid" + "=" + mid, null, null, null, null, null);
I however get an sqlite exception when I try to do the same. Any ideas?
The exception is -
Error: android.database.sqlite.SQLiteException: no such column: qVEl3: , while compiling: SELECT DISTINCT _id, actionData, mid FROM notifs WHERE mid=qVEl3

You missed the quotations:
SELECT DISTINCT _id, actionData, mid FROM notifs WHERE mid='qVEl3'
So your Android code should be :
Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[]
{ KEY_ROWID, "actionData", "mid" },
"mid=?", new String[] {mid}, null, null, null, null);
(that will prevent SQL injection too)

You'll want to use query parameters to avoid issues with quotes:
Cursor mCursor = mDb.query(
true,
DATABASE_TABLE,
new String[] { KEY_ROWID, "actionData", "mid" },
"mid = ?", // the ? is a placeholder.
new String[] { mid }, // sqlite will put the value of mid into the placeholder above.
null,
null,
null,
null);

Related

Limit number of rows in Cursor query

I need to get contacts from an android phone. so I am using this code:
Cursor phones = getActivity().getContentResolver().query(Phone.CONTENT_URI,
new String[] { Phone.NUMBER, Phone.TYPE },
" DISPLAY_NAME = ?", new String[] { displayName }, null);
while (phones.moveToNext()) {
//do work here
}
I want to tell the cursor to limit the response to 50 with something like "LIMIT 50". Where do I pass that information to the cursor? Please copy and paste my code and make edit there.
There is no limit in Android API, but you can apply it in your code like this:
Cursor phones = getActivity().getContentResolver().query(Phone.CONTENT_URI,
new String[] { Phone.NUMBER, Phone.TYPE },
" DISPLAY_NAME = ?", new String[] { displayName }, null);
for (int i = 0; i < 50 && phones.moveToNext(); ++i) {
// do work here
}
phones.close();
Order by id DESC Limit 1:
db.query("table", null, "column=?", new String[]{"value"}, null, null, "id DESC", "50");
If you have the DB and the table name you can use that, but Content providers don't have a limit argument in their query statement.
mDb = new DBHelper (this);
Cursor c = mDB.getReadableDatase().query(
<Phone_Table_Name>,
new String[] { Phone.NUMBER, Phone.TYPE },
"DISPLAY_NAME = ?",
new String[] { displayName }, null),
null,
null,
null,
"50");

sql exception,no such column exist(android)

I want to fetch record according to item_name..but it gives sql exception no such column: Roti exist.
query of my code is there
db.query(TABLE_1, new String[] { KEY_ITEM_CALORIES }, KEY_ITEM_NAME + "=" + item_name, null, null, null, null, null);
whats is the problem in this query?
you can make like that
Cursor mCursor=db.query(tablename, new String[] {"columnname1","columnname2"},"KEY_ITEM_NAME=?",new String[] { item_name }, null, null,null);
Try this
`Cursor cursor = db.rawQuery
("select * from Table_Name where Column_Name ='"+ value +"'", null)

query method in SQLiteDatabase

I want to know how to use query method that have where clause with like property.
basically what i want is to select all the columns WHERE C_NAME column is LIKE keyWord.
I've tried this, but it doesn't work:
cursor = db.query(TABLE, null, C_NAME, new String[] {"like '"+keyWord+"'"}, null, null, null);
Look at the docs. They say:
selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
so try this:
cursor = db.query(TABLE, C_NAME, new String[] {C_NAME + " like '" + keyWord + "'"}, null, null, null);
Also see this answer as well to see some examples on how to use the selectionArgs (4th argument above). That is where keyWord would have to go.
this query work perfect for me
Cursor cursor = db.query(true, TABLE_NAME, new String[]{ COLUMNS }, ROW + " LIKE ?", new > String[] { "%" + name + "%" }, null, null, null, null);

Android SQLite Query Not Working

I'm trying to search data on database based on 2 conditions (entryId=rowId & category=cat_id) like below
Cursor mCursor = db.query(true, "favorite", new String[] {"_id", "entry"}, "entryId="+ rowId + "& category="+cat_id, null, null, null, null, null);
Its not working. But when I try to do it using only one condition like below then it works well.
Cursor mCursor = db.query(true, "favorite", new String[] {"_id", "entry"}, "entryId="+ rowId, null, null, null, null, null);
Can you help me telling how can I run this query with multiple conditions?
In SQLite & is different from AND, simply use:
Cursor mCursor = db.query(true, "favorite", new String[] {"_id", "entry"},
"entryId="+ rowId + " AND category="+cat_id, null, null, null, null, null);
// Change & to AND... here: ^^^

SQLite syntax error near "%"

I get this error
android.database.sqlite.SQLiteException: near "%": syntax error: , while compiling: SELECT _id, word, meaning FROM todo WHERE KEY_WORD = +%D9%84
for this
database.query(DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_WORD, KEY_MEANING }, "KEY_WORD = " +word, null, null,
null, null,null);
when word is +%D9%84
You have to escape the SQL query. To do this, use the selectionArgs field (right after "KEY_WORD = " +word in your code). It will replace any ? in the selection field with its elements:
database.query(DATABASE_TABLE, new String[] {
KEY_ROWID, KEY_WORD, KEY_MEANING }, KEY_WORD + " = ?", new String[] { word },
null, null, null, null);
For more details, read this.
You have to quote the word with" characters. If you want to use wildcards, you have to use the like comparison operator instead of =.

Categories

Resources