0

我正在通过 web 上的教程尝试使用 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)
4

4 回答 4

1

仔细阅读异常:

SQLiteException: no such column: commentss: , while compiling: SELECT _id, commentss FROM vaib

这告诉我们没有名为 的列commentss。您的列名为comments,而不是commentss...只需修复您的SELECT-statement 中的拼写错误。

于 2012-09-30T12:05:10.903 回答
0

这一切都基于猜测和类似问题..您正在使用库或框架来代表您实现使用反射的数据访问层。

也就是说:
它使用模型类(如类)中的成员变量名称Comment来访问数据库中comments具有相似字段的表。

所以..基于这个假设,错误在以下行:

private String comments;

在你的Comment课上。
它应该是:

private String comment;

在行尾没有 (s)。

让我知道它是否有效。

于 2012-09-30T13:47:23.140 回答
0

该错误表明该错误可能是在 MainActivity.java 中调用时

List<Comment> values = datasource.getAllComments();

使用文件“CommentsDataSource.java”第 56 行中的 getAllComments() 方法:

“在 dao.CommentsDataSource.getAllComments(CommentsDataSource.java:56)”

请检查此行,确保在此行的查询中重用相同的变量名称 DB_COL2。

于 2013-09-29T16:38:56.597 回答
0

我猜你在某处拼错了评论 - >评论,因为这是错误。再次检查您的代码并确保在重新启动之前确实删除了数据库。只是改变

 private static final String DB_NAME = "db_name.db"; 

private static final String DB_NAME = "db_name01.db";

等等以避免混淆 - 并不断增加 01 到 02 等,直到您的数据库正常。

在更改数据库时保留相同的名称可能会给您带来奇怪的错误。

查看您的代码,我在任何地方都找不到评论-因此必须在其他地方或您以前的一些尝试中...

于 2012-09-30T12:01:49.917 回答