SQLiteException:table没有名为x的列

时间:2017-03-16 03:08:21

标签: android sqlite

我只是将对象的字段添加到SQLite数据库中。

以下是添加对象的代码:

DBHandler db = new DBHandler(this);

db.addAlcohol(new Alcohol(1, "13-03-2017", AlcoholType.BEER, 568, 1));
db.addAlcohol(new Alcohol(2, "14-03-2017", AlcoholType.WINE, 175, 2));
db.addAlcohol(new Alcohol(3, "15-03-2017", AlcoholType.BEER, 568, 2));
db.addAlcohol(new Alcohol(4, "16-03-2017", AlcoholType.CIDER, 568, 1));

addAlcohol并创建表格:

// Database version
private static final int DATABASE_VERSION = 1;
// Database name
private static final String DATABASE_NAME = "AlcoholInfo";
// Alcohols table name
private static final String TABLE_ALCOHOLS = "alcohols";
// Alcohols table columns names
private static final String KEY_ID          = "id";
private static final String KEY_DATE        = "date";
private static final String KEY_TYPE        = "type";
private static final String KEY_VOLUME      = "volume";
private static final String KEY_QUANTITY    = "quantity";

@Override
public void onCreate(SQLiteDatabase db) {
    // Date is cursor.getString(1)
    // Units is cursor.getDouble(2);
    String CREATE_ALCOHOL_TABLE = "CREATE TABLE " + TABLE_ALCOHOLS + " (" +
            KEY_ID + " INTEGER PRIMARY KEY, " + KEY_DATE + " TEXT,"
            + KEY_TYPE + " TEXT, " + KEY_VOLUME + " REAL, "
            + KEY_QUANTITY + " REAL" + ")";

    db.execSQL(CREATE_ALCOHOL_TABLE);
}

public void addAlcohol(Alcohol alcohol) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_DATE, alcohol.getDate());
    values.put(KEY_TYPE, alcohol.getAlcoholType().getName());
    values.put(KEY_VOLUME, alcohol.getVolume());
    values.put(KEY_QUANTITY, alcohol.getQuantity());

    // Insert row
    db.insert(TABLE_ALCOHOLS, null, values);
    db.close(); // Closing database connection
}

Alcohol构造函数:

public Alcohol(int id, String date, AlcoholType alcoholType, double volume, double quantity) {
        this.id = id;
        this.date = date;
        this.alcoholType = alcoholType;
        this.volume = volume;
        this.quantity = quantity;
        this.units = (this.volume * this.alcoholType.getAbv() * this.quantity) / 1000;
        split = this.date.split("-");
    }

SQLiteException

E/SQLiteLog: (1) table alcohols has no column named quantity
E/SQLiteDatabase: Error inserting quantity=1.0 date=13-03-2017 volume=568.0 type=Beer
    android.database.sqlite.SQLiteException: table alcohols has no column named quantity (code 1): , while compiling: INSERT INTO alcohols(quantity,date,volume,type) VALUES (?,?,?,?)
       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
       at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
       at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1472)
       at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
       at gauge.soberupp.DBHandler.addAlcohol(DBHandler.java:73)
       at gauge.soberupp.MainActivity.onCreate(MainActivity.java:60)
       at android.app.Activity.performCreate(Activity.java:6679)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
       at android.app.ActivityThread.-wrap12(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:154)
       at android.app.ActivityThread.main(ActivityThread.java:6119)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我尝试修改CREATE_ALCOHOL_TABLE字符串,但找不到任何成功。

任何帮助都将不胜感激。

0 个答案:

没有答案