繁体   English   中英

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

[英]android SQLITE database app crashes oncreate when it runs the getAllComments() method

我正在尝试制作一个您可以一次在数据库中输入两个注释的应用程序,当尝试将所有注释添加到列表中时,oncreate总是崩溃。 非常感激任何的帮助。 先感谢您。

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)

您发布的日志记录没有说出这样的列“名称”,这意味着没有成功创建具有标题为“名称”的列的数据库表“ TABLE_COMMENTS”。 然后从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与“ integer not null”之间没有空格,那么显然它将其视为没有数据类型的单字符串列名称。

看到这里-> COLUMN_NAME +“整数不为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)";

更新:在“注释”和“名称”字段中使用字符串(文本)时,将COLUMN_COMMENT和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):,而编译时:SELECT _id,注释,名称FROM注释”。

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

此外,您对此进行了编码:

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

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

这似乎很简单。

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

这通常是由于您已经在android环境中创建数据库之后更改数据库而引起的。

您可以通过卸载应用程序将其删除。 并重新安装它,这将有效地创建一个新数据库,并应将您转移到工作版本。

让我知道这是否没有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM