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 =.
Related
I currently have the query which selects the corresponding row with the given id
Cursor cursor = db.query(TABLE_MEMOS, new String[] { KEY_ID, KEY_TITLE,
KEY_BODY, KEY_IMPOR, KEY_ALERT }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
I need to cast the column KEY_ID name to _id for it to work with cursors but can't figure out where to add the AS command into the query.
You can specify column aliases in the string array containing column names, e.g.
String[] cols = new String { KEY_ID + "AS _id", KEY_TITLE,
KEY_BODY, KEY_IMPOR, KEY_ALERT };
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)
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);
I have a query that gets data from an sqlite database based on the Day value using a where clause, The data i'm getting is for 'Monday' this data is then populated into a listview. In addition to this i want to order the data by the time in ascending order to form a list with all events on monday from the earliest to latest Start times.
Query by day:
public Cursor getMonday () {
String Monday = "Monday";
return db.query(
DATABASE_TABLE,
new String[] { KEY_ID, KEY_LESSON, KEY_DAY, KEY_START, KEY_END, KEY_LOCATION},
KEY_DAY + "=?",
new String[] {Monday},
null, null, null);
}
I've tried:
public Cursor getMonday () {
String Monday = "Monday";
return db.query(DATABASE_TABLE,
new String[] {KEY_ID, KEY_LESSON, KEY_DAY, KEY_START,KEY_END, KEY_LOCATION},
KEY_DAY + "=?",
new String[] {Monday},
null, null, null,
KEY_START + " ASC");
}
This would usually work but i imagine that the WHERE clause and order by are not in the right order and i dont know how to make them work in unison. Could someone please advise? Thanks.
This is the error im getting:
01-28 14:24:02.738: E/AndroidRuntime(1672): FATAL EXCEPTION: main
01-28 14:24:02.738: E/AndroidRuntime(1672): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.unischeduler/com.example.unischeduler.ScheduleActivity}: java.lang.IllegalArgumentException: invalid LIMIT clauses:Start ASC
Your argument order is incorrect, try this:
public Cursor getMonday () {
String Monday = "Monday";
return db.query(DATABASE_TABLE,
new String[] {KEY_ID, KEY_LESSON, KEY_DAY, KEY_START,
KEY_END, KEY_LOCATION}, KEY_DAY + "=?",
new String[] {Monday}, null, null, KEY_START + " ASC", null);
}
EDIT Proposal to order by hh:mm format:
Take a look to SQLite Date and Time Functions and maybe you could try (I haven't tested it) something like this:
return db.query(DATABASE_TABLE,
new String[] {KEY_ID, KEY_LESSON, KEY_DAY, KEY_START,
KEY_END, KEY_LOCATION}, KEY_DAY + "=?",
new String[] {Monday}, null, null, "strftime('%H:%M', " + KEY_START + ")", null);
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);