0

I've read a lot of threads/questions here about the problem stated above, but no solution seemed to work for me. Here is my database creation:

  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);";

Here I pass a new object to the database:

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

EDIT:

The error message:

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)

And the problem is that as soon as i want to write into the database it throws this error. I want to store a String named question and answer in my cards-database. Sorry for missing these informations!!

Falko
  • 17,076
  • 13
  • 60
  • 105
Tobias Kuess
  • 739
  • 2
  • 14
  • 33

2 Answers2

10

Uninstall your app and install it again. You may have created this table before and this column didn't exist in the earlier version of your app.

When you add a column and don't use onUpdate method, existing tables don't have the new column added. This is not to say that you should use onUpdate every time you change your schema during development- onUpdate should be used when you actually release a new version of your app.

Szymon
  • 42,577
  • 16
  • 96
  • 114
  • 2
    Thank you! Reinstalling of the app fixed it. I was missing a Space in the SQL-Statement which creates the table, found that bug and fixed it. But I did not reinstall. Thank you! – Tobias Kuess Oct 12 '13 at 11:49
  • 1
    @Szymon re installing app solved my problem. Thank you. – Sanjay Bhimani Nov 07 '14 at 04:42
  • "Uninstall your app and install it again" is work for me... but in iOS and swift 3. No matter what platforms you are...this solution works anyway – xhinoda Aug 02 '17 at 15:21
0

when you are not using onUpdate() method then Just change the name of your database everytime whenever you add a new column in the table .

Jordon
  • 191
  • 1
  • 12