[英]SQLite: Select using LIKE '%?%' with rawQuery
I am trying to use a select statement to query some data from the database's table .我正在尝试使用 select 语句从数据库表中查询一些数据。 when i type =?当我输入=? the query succeed , but when i use LIKE %?% instead i got this error in logcat:查询成功,但是当我使用LIKE %?%时,我在 logcat 中收到此错误:
FATAL EXCEPTION: main
Process: com.example.ahmed.bus_time_djerba4, PID: 4178
java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 0 parameters.
and this is my methode that call the database:这是我调用数据库的方法:
public String QuerySQL(String DepartStation,String Destination){
String result="";
SQLiteDatabase db=this.getReadableDatabase();
Cursor c=db.rawQuery("select distinct * from "+TABLE_Name+" where "+Col_3+" LIKE '%?%' and "+Col_4+" LIKE '%?%'", new String[]{DepartStation,Destination});
if(c.getCount()==0) {result="Data not found";c.close();}
else {
while (c.moveToNext()) {
//affichage des lignes
int ligne = c.getInt(1);
String Station = c.getString(2);
String Dest = c.getString(3);
String hours = c.getString(4);
result += "\n" + ligne + "|" + Station + "-" + Dest + " " + hours;
}
c.close();
}
return result;
}
what is the problem ?有什么问题? please请
The Problem is with the SQL query you have used.问题出在您使用的 SQL 查询上。
you are giving ?你在给? A String which is not acceptable for prepare statements.准备语句不可接受的字符串。 select distinct * from table_name where X like '%?%';
is not correct because ?
不正确,因为?
will be a string with double quotation inside a quotation like '%"your_string"%'.将是一个在引号内带有双引号的字符串,例如 '%"your_string"%'。
instead write:而是写:
select distinct * from table_name where X like ?;
and for ?
为?
use " '%your_string%'
".使用“ '%your_string%'
”。 you can apply this to your array of string too.您也可以将其应用于您的字符串数组。
You should use:你应该使用:
Cursor c=db.rawQuery("select distinct * from "+TABLE_Name+" where "+Col_3+" LIKE \"%"+DepartStation+"%\" and "+Col_4+" LIKE \"%"+Destination+"%\"", new String[]{};
instead of this:而不是这个:
Cursor c=db.rawQuery("select distinct * from "+TABLE_Name+" where "+Col_3+" LIKE '%?%' and "+Col_4+" LIKE '%?%'", new String[]{DepartStation,Destination});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.