我正在通过网络上的教程尝试我的Android数据库,但没有得到这样的列例外请帮忙。这是我的代码:
public class MySQLiteHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "db_name.db";
private static final int DB_VERSION = 1;
public static final String DB_TABLENAME="vaib";
//columns
public static final String DB_COL1 = "_id";
public static final String DB_COL2 = "comments";
private static final String DB_CREATE = " create table "
+ DB_TABLENAME + "(" + DB_COL1 +
" integer primary key autoincrement, " + DB_COL2
+ " text );";
public MySQLiteHelper(Context context)
{
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS" + DB_TABLENAME);
onCreate(db);
}
}
//comment.java
//this class will store data in database & display value on UI.
public class Comment {
private long id;
private String comments;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
// Will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
return comments;
}
}
//main activity
public class MainActivity extends ListActivity {
private CommentsDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datasource = new CommentsDataSource(this);
datasource.open();
List<Comment> values = datasource.getAllComments();
// Use the SimpleCursorAdapter to show the
// elements in a ListView
ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.add:
String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
int nextInt = new Random().nextInt(3);
// Save the new comment to the database
comment = datasource.createComment(comments[nextInt]);
adapter.add(comment);
break;
case R.id.delete:
if (getListAdapter().getCount() > 0) {
comment = (Comment) getListAdapter().getItem(0);
datasource.deleteComment(comment);
adapter.remove(comment);
}
break;
}
adapter.notifyDataSetChanged();
}
@Override
protected void onResume() {
datasource.open();
super.onResume();
}
@Override
protected void onPause() {
datasource.close();
super.onPause();
}
}
这是日志消息:
09-30 17:21:42.679: E/AndroidRuntime(30819): FATAL EXCEPTION: main
09-30 17:21:42.679: E/AndroidRuntime(30819): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.databaseapp/com.example.databaseapp.MainActivity}: android.database.sqlite.SQLiteException: no such column: comments: , while compiling: SELECT _id, commentss FROM vaib
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.os.Looper.loop(Looper.java:130)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-30 17:21:42.679: E/AndroidRuntime(30819): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 17:21:42.679: E/AndroidRuntime(30819): at java.lang.reflect.Method.invoke(Method.java:507)
09-30 17:21:42.679: E/AndroidRuntime(30819): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-30 17:21:42.679: E/AndroidRuntime(30819): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-30 17:21:42.679: E/AndroidRuntime(30819): at dalvik.system.NativeStart.main(Native Method)
09-30 17:21:42.679: E/AndroidRuntime(30819): Caused by: android.database.sqlite.SQLiteException: no such column: commentss: , while compiling: SELECT _id, commentss FROM vaib
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1357)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1236)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1190)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1272)
09-30 17:21:42.679: E/AndroidRuntime(30819): at dao.CommentsDataSource.getAllComments(CommentsDataSource.java:56)
09-30 17:21:42.679: E/AndroidRuntime(30819): at com.example.databaseapp.MainActivity.onCreate(MainActivity.java:23)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-30 17:21:42.679: E/AndroidRuntime(30819): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
答案 0 :(得分:1)
仔细阅读例外情况:
SQLiteException: no such column: commentss: , while compiling: SELECT _id, commentss FROM vaib
这告诉我们没有名为commentss
的列。您的列名为comments
,而不是commentss
...只需在SELECT
语句中修正拼写错误。
答案 1 :(得分:0)
我猜你错误评论的地方 - &gt;评论,因为这是错误。再次检查您的代码,确保在重新启动之前真正删除了数据库。只需改变
private static final String DB_NAME = "db_name.db";
到
private static final String DB_NAME = "db_name01.db";
以避免混淆 - 并继续增加01到02等,直到你的数据库正常。
更改数据库时保留相同的名称可能会给您带来奇怪的错误。
查看你的代码我无法在任何地方找到评论 - 所以必须在其他地方或以前的某些尝试...
答案 2 :(得分:0)
这都是基于猜测和类似的问题.. 您正在使用库或框架代表您实现使用反射的数据访问层。
即:
它使用模型类中的成员变量名(如Comment
类)来访问数据库中具有类似字段的表comments
。
所以..基于这个假设,错误在以下一行:
private String comments;
在Comment
课程中
它应该是:
private String comment;
没有(s)在行的末尾。
让我知道它是否有效。
答案 3 :(得分:0)
该错误表示在MainActivity.java中调用时可能出错:
List<Comment> values = datasource.getAllComments();
使用文件“CommentsDataSource.java”中的方法getAllComments(),第56行:
“at dao.CommentsDataSource.getAllComments(CommentsDataSource.java:56)”
请检查此行,确保在此行的查询中重复使用相同的变量名称DB_COL2。