gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:13:32 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,您可以一次在数据库中输入两条评论,当它尝试将所有评论添加到列表中时,它会在创建时不断崩溃。非常感激任何的帮助。预先感谢您。

创建时

 @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;
}
}

日志猫:

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)

最佳答案

每当您更改数据库结构时,请确保增加数据库版本以调用 onUpgrade ,您应该删除旧的数据库结构并回调 onCreate 来重建数据库随着变化。

关于java - android SQLITE 数据库应用程序在运行 getAllComments() 方法时在创建时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29913734/

25 4 0
文章推荐: java - 如何在 Java 代码中正确转义 SOQL 查询中的特殊字符(如 - ')?
文章推荐: jquery获取列表项点击的父ul
文章推荐: jQuery onclick 函数仅适用于第一个
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com