gpt4 book ai didi

android - SQLite 异常 : Unrecognized token when reading from database

转载 作者:IT王子 更新时间:2023-10-29 06:26:58 26 4
gpt4 key购买 nike

我在应用程序中创建了一个 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);

表格截图,只是为了证明oddelek“4c”确实存在: 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 更改为

new String[] { dan, Integer.toString(ura), oddelek }

关于android - SQLite 异常 : Unrecognized token when reading from database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20838233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com