Android SQLiteException syntax error code 1 -
i made database in application, code giving error syntax (code 1) table exception. read questions exception, did said. still doesn't work. couldn't find problem is. there 1 can me?
my code here :
public class notesdatabaseadapter { public static final string key_name = "name"; public static final string key_image = "icon"; public static final string key_date_time = "when"; public static final string key_rowid = "_id"; private static final string tag = "notesdatabaseadapter"; private databasehelper mdbhelper; private sqlitedatabase mdatabase; private static final string database_table = "downloads"; /** * khởi tạo cơ sở dữ liệu */ private static final string database_name = "blogradio.db"; private static final int database_version = 1; private static final string create_download_table = "create table " + database_table + "(" + key_rowid + " integer primary key autoincrement, " + key_name + " text not null, " + key_image + " text not null, " + key_date_time + " text not null);"; private final context mctx; private static class databasehelper extends sqliteopenhelper { databasehelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { log.i("", "abc " + create_download_table); db.execsql(create_download_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(tag, "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop table if exists " + database_table); oncreate(db); } } /** * constructor - takes context allow database * opened/created * * @param ctx * context within work */ public notesdatabaseadapter(context ctx) { this.mctx = ctx; } /** * open notes database. if cannot opened, try create new * instance of database. if cannot created, throw exception * signal failure * * @return (self reference, allowing chained in * initialization call) * @throws sqlexception * if database neither opened or created */ public notesdatabaseadapter open() throws sqlexception { mdbhelper = new databasehelper(mctx); mdatabase = mdbhelper.getwritabledatabase(); return this; } public void close() { mdbhelper.close(); } /** * create new note using title , body provided. if note * created return new rowid note, otherwise return * -1 indicate failure. * * @param title * title of note * @param body * body of note * @return rowid or -1 if failed */ public long createnote(itemdownload item) { contentvalues initialvalues = new contentvalues(); initialvalues.put(key_name, item.getname()); initialvalues.put(key_image, item.geticon()); initialvalues.put(key_date_time, item.getwhen()); return mdatabase.insert(database_table, null, initialvalues); } /** * delete note given rowid * * @param rowid * id of note delete * @return true if deleted, false otherwise */ public boolean deletenote(long rowid) { return mdatabase.delete(database_table, key_rowid + "=" + rowid, null) > 0; } /** * return cursor on list of notes in database * * @return cursor on notes */ // lây toàn bộ các ghi chú để hiển thị lên listview public cursor fetchallnotes() { return mdatabase.query(database_table, new string[] { key_rowid, key_name, key_image, key_date_time }, null, null, null, null, null); } /** * return cursor positioned @ note matches given rowid * * @param rowid * id of note retrieve * @return cursor positioned matching note, if found * @throws sqlexception * if note not found/retrieved */ public cursor fetchnote(long rowid) throws sqlexception { cursor mcursor = mdatabase.query(true, database_table, new string[] { key_rowid, key_name, key_image, key_date_time }, key_rowid + "=" + rowid, null, null, null, null, null); if (mcursor != null) { mcursor.movetofirst(); } return mcursor; } /** * update note using details provided. note updated * specified using rowid, , altered use title , body * values passed in * * @param rowid * id of note update * @param title * value set note title * @param body * value set note body * @return true if note updated, false otherwise */ public boolean updatenote(long rowid, itemdownload item) { contentvalues args = new contentvalues(); args.put(key_name, item.getname()); args.put(key_image, item.geticon()); args.put(key_date_time, item.getwhen()); return mdatabase.update(database_table, args, key_rowid + "=" + rowid, null) > 0; } } mainactivity :
public class requestclass extends activity implements onclicklistener { button start, stop, startactivity; int btnstart, btnstop; public static final string state = "start"; private intent intent; notesdatabaseadapter db; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.request_layout); db = new notesdatabaseadapter(requestclass.this); db.open(); start = (button) findviewbyid(r.id.btnstartservice); stop = (button) findviewbyid(r.id.btnstopservice); startactivity = (button) findviewbyid(r.id.btnstartactivity); start.setonclicklistener(this); stop.setonclicklistener(this); startactivity.setonclicklistener(this); } @override public void onclick(view view) { switch (view.getid()) { case r.id.btnstartservice: intent = new intent(this, serviceclass.class); intent.putextra(state, "http://media3.nhacvietplus.com.vn/upload/cms/nam_2015/thang_5/ngay_7/images/blog-radio389-5.jpg"); // itemdownload item = new itemdownload("blogradio", "xxx", "http://media3.nhacvietplus.com.vn/upload/cms/nam_2015/thang_5/ngay_7/images/blog-radio389-5.jpg"); // db.createnote(item); // db.close(); startservice(intent); break; case r.id.btnstopservice: stopservice(intent); break; case r.id.btnstartactivity: intent = new intent(requestclass.this, responseclass.class); startactivity(i); break; } } @override protected void onresume() { super.onresume(); } @override protected void onpause() { super.onpause(); } @override protected void ondestroy() { super.ondestroy(); } } logcat :
05-14 14:08:41.152: e/sqlitelog(1979): (1) near "when": syntax error 05-14 14:08:41.152: d/androidruntime(1979): shutting down vm 05-14 14:08:41.152: w/dalvikvm(1979): threadid=1: thread exiting uncaught exception (group=0xa61fe908) 05-14 14:08:41.156: e/androidruntime(1979): fatal exception: main 05-14 14:08:41.156: e/androidruntime(1979): java.lang.runtimeexception: unable start activity componentinfo{android.tadev.demodownloadservice/android.tadev.downloadservice.temp.requestclass}: android.database.sqlite.sqliteexception: near "when": syntax error (code 1): , while compiling: create table downloads(_id integer primary key autoincrement, name text not null, icon text not null, when text not null); 05-14 14:08:41.156: e/androidruntime(1979): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 05-14 14:08:41.156: e/androidruntime(1979): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 05-14 14:08:41.156: e/androidruntime(1979): @ android.app.activitythread.access$600(activitythread.java:141) 05-14 14:08:41.156: e/androidruntime(1979): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 05-14 14:08:41.156: e/androidruntime(1979): @ android.os.handler.dispatchmessage(handler.java:99) 05-14 14:08:41.156: e/androidruntime(1979): @ android.os.looper.loop(looper.java:137) 05-14 14:08:41.156: e/androidruntime(1979): @ android.app.activitythread.main(activitythread.java:5041) 05-14 14:08:41.156: e/androidruntime(1979): @ java.lang.reflect.method.invokenative(native method) 05-14 14:08:41.156: e/androidruntime(1979): @ java.lang.reflect.method.invoke(method.java:511) 05-14 14:08:41.156: e/androidruntime(1979): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-14 14:08:41.156: e/androidruntime(1979): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-14 14:08:41.156: e/androidruntime(1979): @ dalvik.system.nativestart.main(native method) 05-14 14:08:41.156: e/androidruntime(1979): caused by: android.database.sqlite.sqliteexception: near "when": syntax error (code 1): , while compiling: create table downloads(_id integer primary key autoincrement, name text not null, icon text not null, when text not null); 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:882) 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:493) 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqlitestatement.<init>(sqlitestatement.java:31) 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqlitedatabase.executesql(sqlitedatabase.java:1663) 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqlitedatabase.execsql(sqlitedatabase.java:1594) 05-14 14:08:41.156: e/androidruntime(1979): @ android.tadev.downloadservice.temp.notesdatabaseadapter$databasehelper.oncreate(notesdatabaseadapter.java:44) 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqliteopenhelper.getdatabaselocked(sqliteopenhelper.java:252) 05-14 14:08:41.156: e/androidruntime(1979): @ android.database.sqlite.sqliteopenhelper.getwritabledatabase(sqliteopenhelper.java:164) 05-14 14:08:41.156: e/androidruntime(1979): @ android.tadev.downloadservice.temp.notesdatabaseadapter.open(notesdatabaseadapter.java:79) 05-14 14:08:41.156: e/androidruntime(1979): @ android.tadev.downloadservice.temp.requestclass.oncreate(requestclass.java:23) 05-14 14:08:41.156: e/androidruntime(1979): @ android.app.activity.performcreate(activity.java:5104) 05-14 14:08:41.156: e/androidruntime(1979): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 05-14 14:08:41.156: e/androidruntime(1979): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 05-14 14:08:41.156: e/androidruntime(1979): ... 11 more
you can't call column "when", since keyword in sqlite
Comments
Post a Comment