2013-10-12 75 views
0

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

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

你能发布错误消息吗? – ssantos

+0

“上述问题”......以上是什么? – mbanzon

+0

@Bhushan aahh ... - 无论如何 - 正如前面提到的错误信息和对预期内容的简短解释会让我们有很长的路要走;-) – mbanzon

回答

10

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

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

+2

谢谢!重新安装该应用程序修复了它。我在创建表的SQL语句中缺少空格,发现该错误并修复了它。但我没有重新安装。谢谢! –

+1

@Szymon重新安装应用程序解决了我的问题。谢谢。 –

+0

“卸载你的应用程序并重新安装”对我来说是工作的......但是在iOS和swift中3.不管你​​是哪种平台......这个解决方案无论如何都可以工作 – xhinoda

0

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