0

我在这里阅读了很多关于上述问题的线程/问题,但似乎没有解决方案对我有用。这是我的数据库创建:

  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)

问题是,一旦我想写入数据库,它就会抛出这个错误。我想在我的卡片数据库中存储一个名为问题和答案的字符串。抱歉错过了这些信息!!

4

2 回答 2

10

卸载您的应用程序并重新安装。您之前可能已经创建了此表,并且此列在您的应用程序的早期版本中不存在。

当您添加列并且不使用onUpdate方法时,现有表不会添加新列。这并不是说onUpdate在开发过程中每次更改架构时onUpdate都应该使用——应该在实际发布应用程序的新版本时使用。

于 2013-10-12T11:39:14.520 回答
0

当您不使用 onUpdate() 方法时,每次在表中添加新列时只需更改数据库的名称。

于 2015-02-20T05:58:44.983 回答