Android SQLite Query Not Working - android

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: ^^^

Related

why rawQuery is working and query isn't working?

I am using query for search but i don't understand why one code is work and another one not work ?!
this is working:
String selectQuery = "SELECT * FROM " + TABLE_Soras
+" WHERE "+KEY_SoraName+" LIKE '%"+soraName+"%'";
Cursor cursor = db.rawQuery(selectQuery,null);
but this isn't get any result:
Cursor cursor = db.query(TABLE_Soras, new String[]{
KEY_SORA_NO, KEY_SoraName}, KEY_SoraName + "=?",
new String[]{soraName}, null, null, null, null);*/
Because in second case % is not appended into value and you didn't add LIKE clause into query. You need to use this:
KEY_SoraName + " like ?", new String[] {"%" +soraName + "%"} ...
Try this
Cursor cursor = db.query(TABLE_QuranSoras, new String[]{
KEY_SORA_NO, KEY_SoraName}, KEY_SoraName+" LIKE '%"+soraName+"%'", null, null, null, null);
Cursor cursor = your_database.query(MY_TABLE, new String[] {"first_column","second_column"},your_column + " LIKE '%"+your_criteria+"%'", null, null, null, null);
Try something like this and it will probably work!

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

Inappropiate search through sqlite database from android

This query seems to work fine:
public Cursor getOneWine(long id) {
return database.query("vino", null, "_id=" + id, null,
null, null, null);
}
But when I try to change "_id"+id to "name="+name it does not work! also tried this:
public Cursor getSearchWine(String name) {
return database.query("vino", new String[] {"_id", "name"}, "name="+name, null,
null, null, "name");
}
which is working well when I want a cursor from all wines.
public Cursor getAllWines() {
return database.query("vino", new String[] {"_id", "name"}, null, null,
null, null, "name");
}
Also checked the Android documentation but didn't find a solution.
In SQL, strings must be quoted:
db.query("vino", new String[] {"_id", "nombre"}, "name='" + name + "'", ...);
However, this will cause problems if the name contains '; it is recommended to use parameters for strings instead:
db.query("vino", new String[] {"_id", "nombre"}, "name=?", new String[] {name}, ...);

Unable to fetch an sqlite record

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

data type mismatch error when using ORDER BY

I've got an android app using a local sqlite database.
private SQLiteDatabase mDb;
when I run this query I get my Cursor over rows with pid equal to id, as desired:
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, null);
when I run the following query, aiming to get that same result set, ordered by pid I get "android.database.sqlite.SQLiteException: datatype mismatch"
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, KEY_PID+" DESC");
Any ideas?
It looks like you got just a little mixed up. According to the SQLiteDatabase.query documentation, the last argument is the LIMIT clause. The second to last is the ORDER BY clause.
Cursor query (boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy, // <-- ORDER BY
String limit)
EDIT
But, there is also another SQLiteDatabase.query where ORDER BY would be last
Cursor query (String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy)
This worked for me
String filter = MySQLiteHelper.JOB_ID + "=" + Integer.toString(jobID);
String orderBy = MySQLiteHelper.LOG_TIME + " DESC";
Cursor cursor = database.query(MySQLiteHelper.LOG_TABLE_NAME, logTableColumns,
filter, null, null, null, orderBy);
KEY_PID + " = " + "'" + id + "'"
Since Orderby is the second last parameter in the query;
your query would be like this
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, KEY_PID+" DESC", null);
If I correctly understood your problem, try this.
String[] columns = new String[] {KEY_PID, KEY_TID};
String where = KEY_PID + " = " + Integer.toString(id);
String orderBy = KEY_PID + " DESC";
Cursor cursor = mDb.query(PT_TABLE, columns, where, null, null, null, orderBy);

Categories

Resources