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:アプリケーションを作成できません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 に答える