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

时间:2013-12-30 11:05:39

标签: android sqlite android-sqlite

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

3 个答案:

答案 0 :(得分:21)

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 }

答案 1 :(得分:1)

String selection = SQLiteHelper.DAN+"="+dan+" and "+SQLiteHelper.URA+"="+ura+" and "+SQLiteHelper.ODDELEK+"='"+oddelek+"'";

替换为此并检查

答案 2 :(得分:0)

问题仅归因于缺少引号: -

示例

 db.delete(TABLE_NA,E, ADDRESS + "= '" + address + "'", null);

请查看以下字段 +" ='" +地址+"'"

  

{问题,因为地址是字符串,所以它应该在'地址' }