tags:

views:

67

answers:

1

I am trying to write a select query from one single table in SQLite DB in my app where data exists.

This is the SQL.table has these columns - id(INTEGER), start_time(INTEGER), category(TEXT)

select category from tuuserid_pref where id = (select max(id) from tuuserid_pref where start_time < '1242'); by the way I want to bind the value shown in 1242 which I have in an Integer variable.

I have written selects and inserts and updates without binding parameters and they work using db.execSQL and db.rawQuery functions but just cannot figure the binding parameters out.

Any help is much appreciated.

+1  A: 

You need to modify your select statement like so:

select category from tuuserid_pref where id = 
(select max(id) from tuuserid_pref where start_time < ?);

Then use the following rawQuery call:

rawQuery(String sql, String[] selectionArgs)

Pass your variable into the second parameter wrapped in a String [], which should give you something like the following statement:

db.rawQuery(sqlStatement, new String[] { String.valueOf(myInt) });

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

McStretch
does the db.execSQL function call return a cursor object in this case?
Aakash
Sorry Aakash, in this case you want to call rawQuery(String sql, String[] selectionArgs), which will return you a Cursor object. I'll update my answer to reflect this change.
McStretch
I did that and it works...thanks.I used db.query instead of rawQyery
Aakash
db.query(table, categorycol, catselection, catselectionArgs, null, null, null);
Aakash