Android 我在“附近”得到了错误SQLiteLog&引用;
我得到了错误“SQLiteLog:(1)靠近”?:语法错误“,有人知道我的代码出了什么问题吗?我已经准备好尝试尝试错误来解决这个问题,但仍然没有结果 以下是错误日志的标题:Android 我在“附近”得到了错误SQLiteLog&引用;,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我得到了错误“SQLiteLog:(1)靠近”?:语法错误“,有人知道我的代码出了什么问题吗?我已经准备好尝试尝试错误来解决这个问题,但仍然没有结果 以下是错误日志的标题: E/SQLiteLog: (1) near "?": syntax error E/AndroidRuntime: FATAL EXCEPTION: IntentService[SyncVideoServices] Process: com.sbi.mvicall, PID: 11872
E/SQLiteLog: (1) near "?": syntax error
E/AndroidRuntime: FATAL EXCEPTION: IntentService[SyncVideoServices]
Process: com.sbi.mvicall, PID: 11872
android.database.sqlite.SQLiteException: near "?": syntax
error (code 1): , while compiling: SELECT _id, caller_id, name, msisdn, pic,
status, video, profiles FROM tbl_caller WHERE msisdn IN (?
以下是我的java代码:
public String [] getRegisteredMSISDN(String [] msisdn){
if(msisdn != null && msisdn.length > 0){
String whereStatement = "";
for(int i=0; i<msisdn.length; i++){
whereStatement += ",";
if(i==0){
whereStatement += "(?";
}
else if(i==msisdn.length-1){
whereStatement += "?)";
}
else{
whereStatement += "?";
}
}
whereStatement = whereStatement.substring(1);
whereStatement = UserTable.UserEntry.COLUMN_NAME_MSISDN + " IN " + whereStatement;
SQLiteDatabase database = DBHelper
.getInstance(applicationContext)
.getReadableDatabase();
// =============== The error pointing code below ==============
Cursor cursor = database.query(UserTable.UserEntry.TABLE_NAME, UserTable.ALL_COLUMNS, whereStatement, msisdn,
null,
null,
null);
String [] result = null;
if (cursor.getCount() > 0) {
int index = 0;
result = new String[cursor.getCount()];
while (cursor.moveToNext()) {
result[index] = cursor.getString(cursor.getColumnIndex(UserTable.UserEntry.COLUMN_NAME_MSISDN));
index++;
}
}
cursor.close();
return result;
}
return null;
}
public String[]getRegisteredMSISDN(String[]msisdn){
如果(msisdn!=null&&msisdn.length>0){
字符串whereStatement=“”;
对于(int i=0;i 0){
int指数=0;
结果=新字符串[cursor.getCount()];
while(cursor.moveToNext()){
结果[index]=cursor.getString(cursor.getColumnIndex(UserTable.UserEntry.COLUMN_NAME_MSISDN));
索引++;
}
}
cursor.close();
返回结果;
}
返回null;
}
检查您的if..else
建筑whereStatement
声明。如果您的msisdn
只有一个元素,您将在whereStatement
中获得(?
)
不要将(
)添加到第一个和)
添加到最后一个,只需在循环前添加(
),在循环后添加)
。此外,只有在whereStatement.length>0
问题得到解释时,才需要这样做
使用以下各项:-
String whereStatement = "";
for(int i=0; i<msisdn.length; i++){
whereStatement += ",";
if(i==0){
whereStatement += "(?";
}
else if(i==msisdn.length-1){
whereStatement += "?)";
}
else{
whereStatement += "?";
}
}
测试 以下内容用于测试:-
for (int i=0; i < 10; i++) {
String[] msisdn = new String[i+1];
for (int j=0; j < msisdn.length; j++) {
msisdn[j] = "Test" + String.valueOf(j);
}
buildWhereStatement(msisdn);
}
结果是:-
你能在
SQLiteDatabase=DBHelper
之前记录你的whereStatement
吗?错误会出现在这一行吗?whereStatement=UserTable.UserEntry.COLUMN\u NAME\u MSISDN+“IN”+whereStatement;@Asqa,为什么你不能按照问题中的说明来做呢?对不起,因为这段代码是从其他开发人员那里继承下来的,我还在学习SQLite@Asqa,那么更重要的是,您必须了解该代码的功能。您还不清楚什么?您是否尝试过自己调试并解决它?
for (int i=0; i < 10; i++) {
String[] msisdn = new String[i+1];
for (int j=0; j < msisdn.length; j++) {
msisdn[j] = "Test" + String.valueOf(j);
}
buildWhereStatement(msisdn);
}
public void buildWhereStatement(String[] msisdn) {
String whereStatement = "";
if (msisdn != null && msisdn.length > 0) {
whereStatement = "(";
for (int i = 0; i < msisdn.length; i++) {
if (i > 0) {
whereStatement += ",";
}
whereStatement += "?";
}
whereStatement += ")";
}
Log.d("WHERESTMNT",whereStatement);
}
04-06 10:08:22.064 1367-1367/? D/WHERESTMNT: (?)
(?,?)
(?,?,?)
(?,?,?,?)
(?,?,?,?,?)
(?,?,?,?,?,?)
(?,?,?,?,?,?,?)
(?,?,?,?,?,?,?,?)
(?,?,?,?,?,?,?,?,?)
(?,?,?,?,?,?,?,?,?,?)