android SQLITE数据库应用程序在运行getAllComments()方法时崩溃oncreate

时间:2015-04-28 08:04:22

标签: java android sqlite

我正在尝试创建一个可以同时在数据库中输入两条注释的应用程序,它会在尝试将所有注释添加到列表时不断创建崩溃。非常感激任何的帮助。提前谢谢你。

的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 a new data manager objects
    datasource = new CommentsManageData(this);
    datasource.open(); //Create or open the database
    List<Comment> values = datasource.getAllComments();
    // use the SimpleCursorAdapter to show elements in a ListView
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
        android.R.layout.simple_list_item_1,values);
    setListAdapter(adapter);
  }

 //This retrieves data from the database and puts it into an ArrayList
  public List<Comment> getAllComments() {
        List<Comment> comments = new ArrayList<Comment>();
        //Retrieve all comments - returns a cursor positioned 
        //over first item in the results     
        Cursor cursor = database.query(CommentsSQLiteHelper.TABLE_COMMENTS,
          allColumns, null, null, null, null, null);
        cursor.moveToFirst(); //Just in case it wasn't there already
        while (!cursor.isAfterLast()) {
            Comment comment = cursorToComment(cursor);
            comments.add(comment);//Add the comment

            cursor.moveToNext(); // move to the next item in results
        }
        cursor.close(); // make sure to close the cursor
        return comments;
      }

SQLiteOpenHelper:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/*
 * This class is responsible for creating the database. 
 * It also defines several constants for the table name and the table columns
 * This could be a 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 this method
      public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
      }

      //The onUpgrade() method will simply delete all existing data and re-create the table.
    //Must override this method
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(CommentsSQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all 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 to 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 FROM comments
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.os.Looper.loop(Looper.java:136)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at java.lang.reflect.Method.invokeNative(Native Method)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at java.lang.reflect.Method.invoke(Method.java:515)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at 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 FROM comments
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at cct.mad.lab.CommentsManageData.getAllComments(CommentsManageData.java:33)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at cct.mad.lab.CommentsApp.onCreate(CommentsApp.java:22)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.app.Activity.performCreate(Activity.java:5231)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-28 04:10:36.068: E/AndroidRuntime(1332):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

4 个答案:

答案 0 :(得分:1)

您发布的logcat表示没有这样的列“名称”,这意味着您的数据库表“TABLE_COMMENTS”具有标题为“name”的列未成功创建。从您的CommentsSQLiteHelper类中,您可以像这样创建表:

// 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)";

在这里,如果你发现COLUMN_NAME&amp;之间没有空格。 “integer not null”的开头,因此它显然将其作为单个字符串列名称,没有数据类型。

  

见这里 - &gt; COLUMN_NAME +“integer not null)”; (在数据类型整数开始之前没有空格)

因此,解决方法是在“integer not null”开始之前放置空格。复制粘贴代码:

 // 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)";
  

更新:正如您在“评论”中使用字符串(文字)&amp; “名称”字段更改COLUMN_COMMENT&amp;的数据类型COLUMN_NAME从整数到文本(字符串)。

使用以下代码:

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)";

答案 1 :(得分:0)

您的错误是:“没有这样的列:名称(代码1):,编译时:SELECT _id,comment,name FROM comments”。

您的数据库中不存在列名。

此外,你编码了这个:

  // 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)";

由于您要存储注释,为什么要使用整数?它应该是这样的文字:

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table "
      + TABLE_COMMENTS + " (" + 
      COLUMN_ID + " integer primary key autoincrement, " +
      COLUMN_COMMENT + " text, "+
      COLUMN_NAME + " text)";

答案 2 :(得分:0)

每当您对数据库结构进行更改时,请确保增加数据库版本以调用onUpgrade,您应该删除旧数据库结构并回调onCreate以使用更改重建数据库。

答案 3 :(得分:-2)

这看起来很简单。

您有一个错误,指出该列不存在于您的数据库中。

这通常是由您在Android环境中创建数据库之后更改数据库引起的。

您可以通过卸载应用删除此功能。并重新安装它,这将有效地创建一个新的数据库,并应将您带到一个工作版本。

如果这没有帮助,请告诉我。