SQLiteException:从数据库读取时无法识别的标记

2024-01-28

我已经在应用程序内创建了一个 SQLite 数据库,填充了它,现在我尝试从中读取数据。该应用程序不断崩溃,这是我收到的 logcat:

12-30 05:53:18.008: E/AndroidRuntime(6205): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testparsing/com.example.testparsing.Urnik}: android.database.sqlite.SQLiteException: unrecognized token: "4c" (code 1): , while compiling: SELECT predmet FROM predmeti WHERE dan=PONEDELJEK and ura=2 and oddelek=4c

从数据库读取的函数:

Cursor getSubject(String dan, int ura, String oddelek){
    String[] columnNames = new String[1];
    columnNames[0] = SQLiteHelper.PREDMET;
    String selection = SQLiteHelper.DAN+"="+dan+" and "+SQLiteHelper.URA+"="+ura+" and "+SQLiteHelper.ODDELEK+"="+oddelek;
    open();
    return db.query(
            SQLiteHelper.IME_TABELE, 
            columnNames, 
            selection, 
            null, null, null, null);
}

我如何尝试阅读:

TextView tw1p = (TextView) findViewById(R.id.tview1p);
DatabaseHandler db = new DatabaseHandler(getApplicationContext());

Cursor c = db.getSubject("PONEDELJEK", 2, "4c");
String predmet = c.getString(c.getColumnIndex(SQLiteHelper.PREDMET));
tw1p.setText(predmet);

A screenshot of table, just to prove that oddelek "4c" in fact does exist: enter image description here


SELECT predmet FROM predmeti WHERE dan=PONEDELJEK and ura=2 and oddelek=4c

您需要引用字符串文字,例如:

SELECT predmet FROM predmeti WHERE dan='PONEDELJEK' and ura=2 and oddelek='4c'

但最好使用?文字占位符:

SELECT predmet FROM predmeti WHERE dan=? and ura=? and oddelek=?

并改变你的null selectionArgs to

new String[] { dan, Integer.toString(ura), oddelek }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SQLiteException:从数据库读取时无法识别的标记 的相关文章

随机推荐