Android - ArrayList<>包含来自语言环境数据库的数据的对象无法加载?

时间:2015-04-26 16:11:59

标签: android sqlite arraylist

我正在开发从SQLite数据库加载数据的Android应用程序。我曾尝试从MainActivity打开我的数据库,没有问题。当我从我的SQLController调用一个方法时,它收到错误(应用程序未打开)。

SQLController:

public class SQLController {
private DBHelper dbhelper;
private Context ourcontext;
private SQLiteDatabase database;

public SQLController(Context c) {
    ourcontext = c;
}

public SQLController open() throws SQLException {
    dbhelper = new DBHelper(ourcontext);
    database = dbhelper.getWritableDatabase();
    return this;

}

public void close() {
    dbhelper.close();
}

public ArrayList<Crops> getCrops(){
    ArrayList<Crops> listCrops = new ArrayList<>();

    String[] allColumns = new String[] { DBHelper.CROPS_ID, DBHelper.IS_FAVOURITE,
            DBHelper.ID_NAME, DBHelper.EN_NAME, DBHelper.SPECIES, DBHelper.GENUS,
            DBHelper.FAMILY, DBHelper.ORDER, DBHelper.CLASS, DBHelper.DIVISION,
            DBHelper.KINGDOM, DBHelper.CATEGORY_ID, DBHelper.PICT};

    Cursor c = database.query(DBHelper.TABLE_CROPS,allColumns,null,null,null,null,null);

    if (c != null && c.moveToFirst()) {
        do {
            Crops crops = new Crops();
            crops.setId(c.getLong(c.getColumnIndex(DBHelper.CROPS_ID)));
            crops.setIdNname(c.getString(c.getColumnIndex(DBHelper.ID_NAME)));
            crops.setIsFavourite(c.getLong(c.getColumnIndex(DBHelper.IS_FAVOURITE)));
            crops.setEnName(c.getString(c.getColumnIndex(DBHelper.EN_NAME)));
            crops.setSpecies(c.getString(c.getColumnIndex(DBHelper.SPECIES)));
            crops.setGenus(c.getString(c.getColumnIndex(DBHelper.GENUS)));
            crops.setFamily(c.getString(c.getColumnIndex(DBHelper.FAMILY)));
            crops.setOrder(c.getString(c.getColumnIndex(DBHelper.ORDER)));
            crops.setClasss(c.getString(c.getColumnIndex(DBHelper.CLASS)));
            crops.setDivision(c.getString(c.getColumnIndex(DBHelper.DIVISION)));
            crops.setCategoryId(c.getLong(c.getColumnIndex(DBHelper.CATEGORY_ID)));
            crops.setPict(c.getString(c.getColumnIndex(DBHelper.PICT)));

            listCrops.add(crops);
        } while (c.moveToNext());
    }

    if (c != null) {
        c.close();
    }
    return listCrops;
}
}

测试方法的MainActivity:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dbcont = new SQLController(this);
    try {
        dbcont.open();
        cropses = dbcont.getCrops();
        dbcont.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

当我删除cropses = dbcont.getCrops();时,应用程序正在运行。

更新: Logcat:

04-27 00:37:15.245    2003-2003/com.mundane.hortipedia E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.mundane.hortipedia, PID: 2003
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mundane.hortipedia/com.mundane.hortipedia.MainActivity}: android.database.sqlite.SQLiteException: near "order": syntax error (code 1): , while compiling: SELECT _id, is_favourite, id_name, en_name, species, genus, family, order, class, division, kingdom, category_id, pict FROM crops
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: android.database.sqlite.SQLiteException: near "order": syntax error (code 1): , while compiling: SELECT _id, is_favourite, id_name, en_name, species, genus, family, order, class, division, kingdom, category_id, pict FROM crops
        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.SQLiteQuery.<init>(SQLiteQuery.java:37)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
        at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
        at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
        at com.mundane.hortipedia.db.SQLController.getCrops(SQLController.java:42)
        at com.mundane.hortipedia.MainActivity.onCreate(MainActivity.java:54)
        at android.app.Activity.performCreate(Activity.java:5933)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)

1 个答案:

答案 0 :(得分:0)

您无法使用order。这个词是为SQLite系统保留的。

这是保留的SQLite单词的list。确保你没有用那里的任何东西命名你的桌子或柱子。