java - Problem with SQL Query Android Where Clause -
i have following code creates table. insert data looks this
_id date recordname total -------------------------------------- 7 2011 testmaxrecord 5 java code:
public static final string key_rowid = "_id"; public static final string key_date = "date"; public static final string key_total = "total"; public static final string key_record_name = "recordname"; private static final string database_create_records = "create table " + database_table_records + " (" + key_rowid + " integer primary key autoincrement, " + key_date + " text not null, " + key_record_name + " text not null, " + key_total + " text not null);"; public cursor getrecord(string name) throws sqlexception { return mdb.query(true, database_table_records, new string[] {key_rowid, key_date, key_record_name, key_total}, key_record_name + " = " + name, null, null, null, null, null); } it throws exception every time name = "testmaxrecord" (despite there being data in there) following error
android.database.sqlite.sqliteexception: no such column: testmaxrecord: , while compiling: select distinct _id, date, recordname, total records recordname=testmaxrecord
which looks me looking column title testmaxrecord. android newbie copied portion pretty example (it using int though). there difference between using int , strings on query?
you need put single quotes around value, otherwise treat column.
key_record_name + " = '" + name + "'" note: solution open sql injection, should use parameter placeholders, , pass values via selectionargs argument:
public cursor getrecord(string name) throws sqlexception { return mdb.query(true, database_table_records, new string[] {key_rowid, key_date, key_record_name, key_total}, key_record_name + " = ?", new string[] {name}, null, null, null, null); } alternatively, sqlitequerybuilder
Comments
Post a Comment