0
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

4

1 回答 1

2

您的CREATE TABLE命令不起作用,因为您在结束之前一个逗号)

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

于 2012-10-30T10:09:12.813 回答