java - Issue with android sql-query..."no such column"? -
i want use sql database store records game. don't have clue sql. have class "records" should manage in , output of records. additionally have class sqldatabasehelper provides sql-database.
my problem following line:
crsrecord = sqlitedatabase.rawquery(query_get_record + category, null);
i got error "no such column: sys103" "sys103" name of category. don't know why can read. have idea?
sql table creation:
create table records ( id integer primary key autoincrement, category varchar(30) not null, displaytime varchar(12) not null, recordtime varchar(10) not null);
i guess writing works reading doesn't work.
public class records { private sqliteopenhelper sqliteopenhelper; private sqlitedatabase sqlitedatabase; private static final string insert_new_record = "insert records(category, displayrecord, timerecord) values("; private static final string query_get_record = "select * records category = "; public records(context context){ sqliteopenhelper = new sqldatabasehelper(context); sqlitedatabase = sqliteopenhelper.getwritabledatabase(); } public void addrecord(string category, string displaytime, string timerecord){ contentvalues data = new contentvalues(); data.put("category", category); data.put("displaytime", displaytime); data.put("recordtime", timerecord); sqlitedatabase.insert("records", null, data); // sqlitedatabase.execsql(insert_new_record + category + ", " + strtime + ", " + dbltime + ");"); } public string[] getrecord(string category){ string[] record = new string[3]; cursor crsrecord; try{ crsrecord = sqlitedatabase.rawquery(query_get_record + category, null); }catch(sqliteexception e){ log.d("database", e.getmessage()); string[] nullrecord = {category, "00:00.0", "0"}; return nullrecord; } int i=0; while(crsrecord.movetonext()){ record[i] = crsrecord.getstring(0); i++; } return record; } } public class sqldatabasehelper extends sqliteopenhelper { private context context; public sqldatabasehelper(context context){ super( context, context.getresources().getstring(r.string.dbname), null, integer.parseint(context.getresources().getstring(r.string.version))); this.context=context; } @override public void oncreate(sqlitedatabase db) { for(string sql : context.getresources().getstringarray(r.array.create)){ db.execsql(sql); } log.d("database", "creat succesfully"); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } }
my method data out of database, reason columnindex alway -1:
public string[] getrecord(string category){ string[] record = new string[3]; cursor crsrecord; crsrecord = sqlitedatabase.rawquery(query_get_record, new string[]{ category } ); int i=0; crsrecord.movetofirst(); while(!crsrecord.isafterlast()){ // instead of using int literal colum index, // use getcolumnindex method int index = crsrecord.getcolumnindex(category); if (index == -1) { string[] nullrecord = {category, "00:00.0", "0"}; return nullrecord; } else { record[i] = crsrecord.getstring(index); i++; } crsrecord.movetonext(); } while(crsrecord.movetonext()){ record[i] = crsrecord.getstring(0); i++; } return record; }
you'll need escape parameters.
as is, code executes query:
select * records category = sys103
that's not valid sql. should this:
select * records category = 'sys103'
and need escape apostrophes. you'd better off relying on rawquery
escape parameters:
private static final string query_get_record = "select * records category = ?";
and
crsrecord = sqlitedatabase.rawquery(query_get_record, new string[]{ category } );
Comments
Post a Comment