i trying make app can enter 2 comments database @ once, keeps crashing oncreate when tries add comments list. appreciated. thank in advance.
oncreate
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.comments); etcomm = (edittext) findviewbyid(r.id.etcomment); etname = (edittext) findviewbyid(r.id.etname); //create new data manager objects datasource = new commentsmanagedata(this); datasource.open(); //create or open database list<comment> values = datasource.getallcomments(); // use simplecursoradapter show elements in listview arrayadapter<comment> adapter = new arrayadapter<comment>(this, android.r.layout.simple_list_item_1,values); setlistadapter(adapter); } //this retrieves data database , puts arraylist public list<comment> getallcomments() { list<comment> comments = new arraylist<comment>(); //retrieve comments - returns cursor positioned //over first item in results cursor cursor = database.query(commentssqlitehelper.table_comments, allcolumns, null, null, null, null, null); cursor.movetofirst(); //just in case wasn't there while (!cursor.isafterlast()) { comment comment = cursortocomment(cursor); comments.add(comment);//add comment cursor.movetonext(); // move next item in results } cursor.close(); // make sure close cursor return comments; }
sqliteopenhelper:
import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; import android.util.log; /* * class responsible creating database. * defines several constants table name , table columns * private class within commentsdatasource */ public class commentssqlitehelper extends sqliteopenhelper {//note subclass public static final string table_comments = "comments"; public static final string column_id = "_id"; public static final string column_comment = "comment"; public static final string column_name = "name"; private static final string database_name = "commments.db"; private static final int database_version = 1; // database creation sql statement private static final string database_create = "create table " + table_comments + "(" + column_id + " integer primary key autoincrement, " + column_comment + " integer not null, "+ column_name + "integer not null)"; public commentssqlitehelper (context context) { super(context, database_name, null, database_version); } //must override method public void oncreate(sqlitedatabase database) { database.execsql(database_create); } //the onupgrade() method delete existing data , re-create table. //must override method public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { log.w(commentssqlitehelper.class.getname(), "upgrading database version " + oldversion + " " + newversion + ", destroy old data"); db.execsql("drop table if exists " + table_comments); oncreate(db); } } comment object code public class comment { private long id; private string comment; private string name; public long getid() { return id; } public void setid(long id) { this.id = id; } public string getcomment() { return comment; } public void setcomment(string comment) { this.comment = comment; } public string getname() { return name; } public void setname(string name) { this.name = name; } }
logcat:
04-28 04:10:35.998: e/sqlitelog(1332): (1) no such column: name 04-28 04:10:36.068: e/androidruntime(1332): fatal exception: main 04-28 04:10:36.068: e/androidruntime(1332): process: cct.mad.lab, pid: 1332 04-28 04:10:36.068: e/androidruntime(1332): java.lang.runtimeexception: unable start activity componentinfo{cct.mad.lab/cct.mad.lab.commentsapp}: android.database.sqlite.sqliteexception: no such column: name (code 1): , while compiling: select _id, comment, name comments 04-28 04:10:36.068: e/androidruntime(1332): @ android.app.activitythread.performlaunchactivity(activitythread.java:2195) 04-28 04:10:36.068: e/androidruntime(1332): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) 04-28 04:10:36.068: e/androidruntime(1332): @ android.app.activitythread.access$800(activitythread.java:135) 04-28 04:10:36.068: e/androidruntime(1332): @ android.app.activitythread$h.handlemessage(activitythread.java:1196) 04-28 04:10:36.068: e/androidruntime(1332): @ android.os.handler.dispatchmessage(handler.java:102) 04-28 04:10:36.068: e/androidruntime(1332): @ android.os.looper.loop(looper.java:136) 04-28 04:10:36.068: e/androidruntime(1332): @ android.app.activitythread.main(activitythread.java:5017) 04-28 04:10:36.068: e/androidruntime(1332): @ java.lang.reflect.method.invokenative(native method) 04-28 04:10:36.068: e/androidruntime(1332): @ java.lang.reflect.method.invoke(method.java:515) 04-28 04:10:36.068: e/androidruntime(1332): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) 04-28 04:10:36.068: e/androidruntime(1332): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) 04-28 04:10:36.068: e/androidruntime(1332): @ dalvik.system.nativestart.main(native method) 04-28 04:10:36.068: e/androidruntime(1332): caused by: android.database.sqlite.sqliteexception: no such column: name (code 1): , while compiling: select _id, comment, name comments 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqliteconnection.nativepreparestatement(native method) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqliteconnection.acquirepreparedstatement(sqliteconnection.java:889) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqliteconnection.prepare(sqliteconnection.java:500) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqlitesession.prepare(sqlitesession.java:588) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqliteprogram.<init>(sqliteprogram.java:58) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqlitequery.<init>(sqlitequery.java:37) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqlitedirectcursordriver.query(sqlitedirectcursordriver.java:44) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqlitedatabase.rawquerywithfactory(sqlitedatabase.java:1314) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqlitedatabase.querywithfactory(sqlitedatabase.java:1161) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1032) 04-28 04:10:36.068: e/androidruntime(1332): @ android.database.sqlite.sqlitedatabase.query(sqlitedatabase.java:1200) 04-28 04:10:36.068: e/androidruntime(1332): @ cct.mad.lab.commentsmanagedata.getallcomments(commentsmanagedata.java:33) 04-28 04:10:36.068: e/androidruntime(1332): @ cct.mad.lab.commentsapp.oncreate(commentsapp.java:22) 04-28 04:10:36.068: e/androidruntime(1332): @ android.app.activity.performcreate(activity.java:5231) 04-28 04:10:36.068: e/androidruntime(1332): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 04-28 04:10:36.068: e/androidruntime(1332): @ android.app.activitythread.performlaunchactivity(activitythread.java:2159)
the logcat posted, says no such column "name" means database table "table_comments" has column title "name" not created successfully. , commentssqlitehelper class, creating table this:
// database creation sql statement private static final string database_create = "create table " + table_comments + "(" + column_id + " integer primary key autoincrement, " + column_comment + " integer not null, "+ column_name + "integer not null)";
here, if you'll observe there no space between column_name & start of "integer not null" taking single string column name no data type.
see here -> column_name + "integer not null)"; (no space before start of datatype integer)
hence, workaround put space before start of " integer not null". copy paste below code:
// database creation sql statement private static final string database_create = "create table " + table_comments + "(" + column_id + " integer primary key autoincrement, " + column_comment + " integer not null, "+ column_name + " integer not null)";
update: using string(text) in "comment" & "name" field change datatype of column_comment & column_name integer text(string).
use below code:
private static final string database_create = "create table " + table_comments + "(" + column_id + " integer primary key autoincrement, " + column_comment + " text not null, "+ column_name + " text not null)";