1
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 = "id";
public static final String ITEM_TYPE = "type";
public static final String ITEM_VALUE = "values";
public static final String ITEM_BUSTYPE = "bustype";
public static final String ITEM_LIGHTTYPE = "lighttype";
public static final String ITEM_SELECT  = "select";

public ListsSQLiteOpenHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
}

@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" +
            ");"
        );
}
}
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 value = 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, value, bustype, lighttype);
            e.setRowId(id);
            e.setSelected(select);
            currentEntries.add(e);
        } while(itemsCursor.moveToNext());
    }

    itemsCursor.close();
}

我收到语法错误

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

我不明白为什么会收到此错误:

10-19 17:01:37.925: E/AndroidRuntime(23164): java.lang.RuntimeException: Unable to create application com.example.abc.EntryManagerApplication: android.database.sqlite.SQLiteException: near "values": 语法错误: , 编译时:SELECT id, type, values, bustype, lighttype, selected FROM light_setting_items ORDER BY selected,type

4

1 回答 1

2

'values' 是一个 SQL 关键字。如果您坚持将其用作字段名称,请将其放在双引号之间。

于 2012-10-21T07:18:52.767 回答