[英]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.