我在这里阅读了很多关于上述问题的线程/问题,但似乎没有解决方案对我有用。这是我的数据库创建:
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)
问题是,一旦我想写入数据库,它就会抛出这个错误。我想在我的卡片数据库中存储一个名为问题和答案的字符串。抱歉错过了这些信息!!