SQLiteException:从数据库读取时无法识别的令牌 [英] SQLiteException: Unrecognized token when reading from database

查看:82
本文介绍了SQLiteException:从数据库读取时无法识别的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在应用程序内部创建了一个SQLite数据库,并对其进行了填充,现在我正尝试从中读取数据.该应用程序不断崩溃,这是我收到的日志猫:

I've created an SQLite database inside application, populated it and now I'm trying to read from it. The app keeps crashing and this is the logcat I receive:

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

该表的屏幕截图,只是为了证明确实存在单数"4c":

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

推荐答案

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

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

You need to quote your string literals, for example:

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

但是最好将?占位符用于文字:

But it's better to use ? placeholder for literals:

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

,然后将您的null selectionArgs更改为

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

这篇关于SQLiteException:从数据库读取时无法识别的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆