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 };
Related
I am currently learning android SQLite database programming. While reading this tutorial from here. I came across a section of code,
getContact()
// Getting single contact
public Contact getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return contact
return contact;
}
My Questions
Question 1
If this code returns the query ,
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
Why do we move the cursor to the first row in this line ?
cursor.moveToFirst();
Question 2
What does this code do ?
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
I read the official documentation but still could not understand. Can some one explain with a simple example ?
Question 1: Because Cursor is set before first row element initially.
Question 2: It gets Strings from row which is set as current in cursor object from columns (every row consists of columns) with numbers 0, 1 and 2. Then it uses them to create contact object.
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 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 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 =.
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);