SQLiteException:没有这样的列itemId :,编译时:

时间:2012-10-30 09:22:37

标签: java android sqlite

public class ListsSQLiteOpenHelper extends SQLiteOpenHelper {

public static final int VERSION = 1;
public static final String DB_NAME  = "lightsettings_db.sqlite";
public static final String ITEMS_TABLE  = "light_setting_items";
public static final String ITEM_ID = "itemId";
public static final String ITEM_TYPE = "itemType";
public static final String ITEM_VALUE = "itemValues";
public static final String ITEM_BUSTYPE = "itemBustype";
public static final String ITEM_LIGHTTYPE = "itemLighttype";
public static final String ITEM_SELECT  = "itemSelected";

public ListsSQLiteOpenHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
    int i=0;
    int y=i;
}

@Override
public void onCreate(SQLiteDatabase db) {
    createTable(db);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}


protected void createTable(SQLiteDatabase db) {
    db.execSQL(
            "create table " + ITEMS_TABLE +" (" +
            ITEM_ID + " integer primary key autoincrement not null, " +
            ITEM_TYPE + " text," +
            ITEM_VALUE + " text," +
            ITEM_BUSTYPE + " text," +
            ITEM_LIGHTTYPE + " text," +
            ITEM_SELECT + " text" +
            ");"
        );
}
}


public class EntryManagerApplication extends Application {

private ArrayList<Entry> currentEntries;

private long lineNumber = 1;

private SQLiteDatabase database;

@Override
public void onCreate() {
    super.onCreate();
    ListsSQLiteOpenHelper helper = new ListsSQLiteOpenHelper(this);
    database = helper.getWritableDatabase();
    if(currentEntries == null){
        loadItems();
    }
}

private void loadItems() {
    currentEntries = new ArrayList<Entry>();

    Cursor itemsCursor = database.query(
            ITEMS_TABLE,
            new String[] {ITEM_ID, ITEM_TYPE, ITEM_VALUE, ITEM_BUSTYPE, ITEM_LIGHTTYPE, ITEM_SELECT},
            null, null, null, null, String.format("%s,%s", ITEM_SELECT, ITEM_TYPE));

    itemsCursor.moveToFirst();
    Entry e;
    if(!itemsCursor.isAfterLast()){
        do{
            long id = itemsCursor.getLong(0);
            String type = itemsCursor.getString(1);
            String values = itemsCursor.getString(2);
            String bustype = itemsCursor.getString(3);
            String lighttype = itemsCursor.getString(4);
            String boolvalue = itemsCursor.getString(5);
            boolean select = Boolean.parseBoolean(boolvalue);
            e = new Entry(id, type, values, bustype, lighttype);
            e.setRowId(id);
            e.setSelected(select);
            currentEntries.add(e);
        } while(itemsCursor.moveToNext());
    }

    itemsCursor.close();
}

public void setCurrentEntries(ArrayList<Entry> currentEntries){
    this.currentEntries = currentEntries;
}

public ArrayList<Entry> getCurrentEntries(){
    return currentEntries;
}

public void addEntry(Entry e){
    assert(e!= null);
    currentEntries.add(e);
}

public long getLineNumber() {
    return lineNumber;
}

public void setLineNumber(long lineNumber) {
    this.lineNumber = lineNumber;
}
}

当我尝试运行此代码时,我收到错误,我不知道请帮忙。

  

android.database.sqlite.SQLiteException:没有这样的列:itemId :,同时编译:SELECT itemId,itemType,itemValues,itemBustype,itemLighttype,itemSelected FROM light_setting_items ORDER BY itemSelected,itemType

1 个答案:

答案 0 :(得分:2)

您的CREATE TABLE命令无效,因为 在结束)之前有逗号。

此外,SQLiteOpenHelper不会为现有数据库调用onCreate方法;您必须增加VERSION并在onUpdate方法中进行必要的更改。 或者,删除并重新安装您的应用。