我在这里已经阅读了很多关于上述问题的线索/问题,但似乎没有任何解决方案对我有用。这是我的数据库创建:
public static final String TABLE_CARDS = "cards";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_QUESTION = "question";
public static final String COLUMN_ANSWER = "answer";
private static final String DATABASE_NAME = "cards.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_CARDS + "("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_QUESTION + " text not null, "
+ COLUMN_ANSWER + " text not null);";
这里我将一个新对象传递给数据库:
public Card createCard(String question, String answer) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_QUESTION, question);
values.put(MySQLiteHelper.COLUMN_ANSWER, answer);
long insertId = database.insert(MySQLiteHelper.TABLE_CARDS, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_CARDS,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Card newCard = cursorToCard(cursor);
cursor.close();
return newCard;
}
编辑:
错误消息:
10-12 07:07:55.503: E/SQLiteDatabase(796): Error inserting answer=testanw question=testqu
10-12 07:07:55.503: E/SQLiteDatabase(796): android.database.sqlite.SQLiteException: table cards has no column named answer (code 1): , while compiling: INSERT INTO cards(answer,question) VALUES (?,?)
10-12 07:07:55.503: E/SQLiteDatabase(796): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
10-12 07:07:55.503: E/SQLiteDatabase(796): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
10-12 07:07:55.503: E/SQLiteDatabase(796): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
10-12 07:07:55.503: E/SQLiteDatabase(796): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
10-12 07:07:55.503: E/SQLiteDatabase(796): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
10-12 07:07:55.503: E/SQLiteDatabase(796): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
10-12 07:07:55.503: E/SQLiteDatabase(796): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
问题是,只要我想写入数据库,它就会抛出此错误。我想在我的卡片数据库中存储一个名为问题和答案的字符串。很抱歉错过了这些信息!!
答案 0 :(得分:10)
卸载您的应用并重新安装。您之前可能已经创建了此表,并且该应用程序的早期版本中不存在此列。
添加列并且不使用onUpdate
方法时,现有表格未添加新列。这并不是说每次在开发过程中更改架构时都应该使用onUpdate
- 当您实际发布新版本的应用时,应该使用onUpdate
。
答案 1 :(得分:0)
当您不使用onUpdate()方法时,只要在表中添加新列,每次都只需更改数据库的名称。