android - Column not found in SQLite databse -
i reated column name called _id, when run , view database in listview using getallrows()
method, shows error saying (no such column: _id (code 1): , while compiling: select distinct _id, eventtitle, date, destination, durationtime, alarmtime event.)
.
below code , error message.
please me , teach happen code.
public class dbhandler{ private static final string tag = "dbhandler"; //field names public static final string column_id = "_id"; public static final string column_eventtitle = "eventtitle"; public static final string column_date = "date"; public static final string column_destination = "destination"; public static final string column_duration = "durationtime"; public static final string column_alarmtime = "alarmtime"; public static final string[] all_keys = new string[]{column_id, column_eventtitle, column_date, column_destination, column_duration,column_alarmtime}; //column number each field name: public static final int col_id = 0; public static final int col_eventtitle = 1; public static final int col_date = 2; public static final int col_destination = 3; public static final int col_duration = 4; public static final int col_alarmtime = 5; //database info: private static final int database_version = 2; private static final string database_name = "sp"; public static final string table_event = "event"; //sql statament create database private static final string database_create_sql = "create table " + table_event + "(" + column_id + " integer primary key autoincrement, " + column_eventtitle + " text not null, " + column_date + " text not null, " + column_destination + " text not null, " + column_duration + " text not null, " + column_alarmtime + " text not null" + ");"; private final context context; private databasehelper mydbhelper; private sqlitedatabase db; public dbhandler(context ctx){ this.context = ctx; mydbhelper = new databasehelper(context); } public dbhandler open(){ db = mydbhelper.getwritabledatabase(); return this; } public void close(){ mydbhelper.close(); } public long insertevent(string eventtitle, string date, string destination, string duration, string alarmtime){ contentvalues contentvalues = new contentvalues(); contentvalues.put(column_eventtitle, eventtitle); contentvalues.put(column_date, date); contentvalues.put(column_destination, destination); contentvalues.put(column_duration, duration); contentvalues.put(column_alarmtime, alarmtime); //insert data database return db.insert(table_event, null, contentvalues); } //delete row database, rowid (primary key) public boolean deleterow(long rowid){ string = column_id + "=" + rowid; return db.delete(table_event, where, null) != 0; } //return data in database public cursor getallrows(){ string = null; cursor c = db.query(true, table_event, all_keys, where, null, null, null,null, null); if(c != null){ c.movetofirst(); } return c; } //get specific row(by rowid) public cursor getrow(long rowid){ string = column_id + "=" + rowid; cursor c = db.query(true, table_event, all_keys, where, null, null, null, null, null); if(c != null){ c.movetofirst(); } return c; } //change existing row equal new data public boolean updateevent(long id, string eventtitle, string date, string destination, string duration,string alarmtime ){ string = column_id + "=" + id; contentvalues newvalues = new contentvalues(); newvalues.put(column_eventtitle, eventtitle); newvalues.put(column_date, date); newvalues.put(column_destination, destination); newvalues.put(column_duration, duration); newvalues.put(column_alarmtime, alarmtime); return db.update(table_event, newvalues, where, null) !=0; } private static class databasehelper extends sqliteopenhelper{ databasehelper(context context){ super(context, table_event, null, database_version); } @override public void oncreate(sqlitedatabase db){ db.execsql(database_create_sql); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion){ log.w(tag, "upgrading application's database version" + oldversion + "to" + newversion + ", destroy old data!"); //destroy old database: db.execsql("drop table if exists" + table_event); oncreate(db); } } }
error:
android.database.sqlite.sqliteexception: no such column: _id (code 1): , while compiling: select distinct _id, eventtitle, date, destination, durationtime, alarmtime event @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:1113) @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:690) @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) @ android.database.sqlite.sqlitequery.<init>(sqlitequery.java:37) @ android.database.sqlite.sqlitedirectcursordriver.query(sqlitedirectcursordriver.java:44) @ android.database.sqlite.sqlitedatabase.rawquerywithfactory(sqlitedatabase.java:1430) @ android.database.sqlite.sqlitedatabase.querywithfactory(sqlitedatabase.java:1277) @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1148) @ com.howard.fyp.dbhandler.getallrows(dbhandler.java:92) @ com.howard.fyp.todayactivity.populatelistview(todayactivity.java:40) @ com.howard.fyp.todayactivity.oncreateview(todayactivity.java:26) @ android.support.v4.app.fragment.performcreateview(fragment.java:1786) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:953) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1136) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:739) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1499) @ android.support.v4.app.fragmentmanagerimpl$1.run(fragmentmanager.java:456) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:157) @ android.app.activitythread.main(activitythread.java:5356) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) @ dalvik.system.nativestart.main(native method)
did add _id
column in second time?
have increase database_version constant value in order make onupgrade()
method fire.
but onupgrade()
method fails destroying old table:
db.execsql("drop table if exists" + table_event);
you need space before table name:
db.execsql("drop table if exists " + table_event);
or, since old table still there, won't create new one.
Comments
Post a Comment