SQLite查询报出异常-问答-阿里云开发者社区-阿里云

开发者社区> a123456678> 正文

SQLite查询报出异常

2016-07-18 13:49:50 1603 1
在数据库中执行一个查询的时候报出异常:
bind or column index out of range
ContentProvider中的查询如下:
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
    case RESPONSEVIEW2_SELECTION_10:
        queryBuilder.setTables(RESPONSES_VIEW);
        String activeUserKey = getActiveUserId();;
        String outerSelection = 
                "(((" + PeerCreatedDataColumns.SHARED + "=" + "" + trueId + "" + ") OR " 
                + "(" + PeerCreatedDataColumns.AUTHORHASH + "=" + "'" + activeUserKey + "'" + ")) AND (" 
                + "(" + ResponsesColumns.PRIMARYMATCH + "= '*'" + ") OR " 
                                    // '*'是程序定义的 , 希望不会和别的有冲突
                + "(" + ResponsesColumns.PRIMARYMATCH + " like '?') OR " 
                + "(" + ResponsesColumns.PRIMARYMATCH + " like '" + selectionArgs[0] + "%') OR " 
                + "(" + ResponsesColumns.PRIMARYMATCH + " like '%" + selectionArgs[0] + "') OR " 
                + "(" + ResponsesColumns.PRIMARYMATCH + " like '%" + selectionArgs[0] + "%'" + ")))";
        Log.d(DEBUG_TAG, "query " + outerSelection);
        Cursor cursor10 = queryBuilder.query(mDB.getReadableDatabase(), projection, outerSelection, selectionArgs,
                null, null, sortOrder);
        cursor10.setNotificationUri(getContext().getContentResolver(), uri);
        Log.e(DEBUG_TAG, "responses_view2 should have better search function");
        return cursor10;
    case ROW_SELECTION_11:
        queryBuilder.setTables(RESPONSES_VIEW);
        Log.e(DEBUG_TAG, "responses_view2 should have better search function");
        queryBuilder.appendWhere(PeerDatabase._ID + "=" + uri.getLastPathSegment());
        Cursor cursor11 = queryBuilder.query(mDB.getReadableDatabase(), projection, selection, selectionArgs, null,
                null, sortOrder);
        cursor11.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor11;
    ...
    }
}
然后cursor loader如下:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    if (search == null) {
        return null;
    } else {
        String[] selectArgs = { search };
        return new CursorLoader(getActivity(), table, createProjection, "?", selectArgs, null);
    }
}
报出错误如下:
12-09 18:25:54.509: E/AndroidRuntime(12461): FATAL EXCEPTION: ModernAsyncTask #2
12-09 18:25:54.509: E/AndroidRuntime(12461): java.lang.RuntimeException: An error occured while executing doInBackground()
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.lang.Thread.run(Thread.java:1019)
12-09 18:25:54.509: E/AndroidRuntime(12461): Caused by: android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x28bd08
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:244)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteQuery.bindString(SQLiteQuery.java:185)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:48)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:330)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:280)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at org.anuvar.plana.m.provider.PeerDataProvider.query(PeerDataProvider.java:285)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.content.ContentProvider$Transport.query(ContentProvider.java:187)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.content.ContentResolver.query(ContentResolver.java:262)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:35)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:240)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
12-09 18:25:54.509: E/AndroidRuntime(12461):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-09 18:25:54.509: E/AndroidRuntime(12461):    ... 4 more
请帮忙指点一下错误的原因,或者还有更好的查询方法,谢谢。
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:57:46
    不要把?包在括号里:
    
    " like '?') OR "
    换成
    
    " like ?) OR "
    如果搜索是用户输入, 还要防止SQL注入攻击:
    
    + "(" + ResponsesColumns.PRIMARYMATCH + " like ?) OR " 
    + "(" + ResponsesColumns.PRIMARYMATCH + " like ?) OR " 
    + "(" + ResponsesColumns.PRIMARYMATCH + " like ?) OR " 
    + "(" + ResponsesColumns.PRIMARYMATCH + " like ?)))";
    创建一个新的selectionArgs参数:
    
    String search = selectionArgs[0];
    String[] matchArgs = { search, search + "%", "%" + search, "%" + search + "%" };
    Cursor cursor10 = queryBuilder.query(mDB.getReadableDatabase(), projection, 
                outerSelection, matchArgs, null, null, sortOrder);
    0 0
相关问答

17

回答

【大咖问答】对话PostgreSQL 中国社区发起人之一,阿里云数据库高级专家 德哥

阿里ACE 彭飞 2019-07-10 09:36:10 962088浏览量 回答数 17

145

回答

【新手入门】云服务器linux使用手册

fanyue88888 2012-11-26 17:14:18 157037浏览量 回答数 145

8

回答

OceanBase 使用动画(持续更新)

mq4096 2019-02-20 17:16:36 333781浏览量 回答数 8

22

回答

爬虫数据管理【问答合集】

我是管理员 2018-08-10 16:37:41 146740浏览量 回答数 22

3

回答

请问postgres实时同步数据到mysql数据库有什么好的方案吗?

toni3 2019-06-29 14:55:43 115765浏览量 回答数 3

2

回答

mySQL数据库报错You have an error in your SQL syntax

落地花开啦 2016-02-14 16:09:24 126049浏览量 回答数 2

38

回答

安全组详解,新手必看教程

我的中国 2017-11-30 15:23:46 256825浏览量 回答数 38

21

回答

请教一下数据量有100万条左右要什么配置?

易网网络 2013-03-27 15:18:02 192310浏览量 回答数 21

78

回答

【2013.9.5修正版图文】新手如何使用阿里云(linux)服务器建站(搬站)

姑苏公子 2013-04-11 00:39:13 107721浏览量 回答数 78

103

回答

云虚拟主机使用教程完整版!!

梦丫头 2015-08-08 13:12:33 107331浏览量 回答数 103
+关注
0
文章
14879
问答
问答排行榜
最热
最新
推荐问答
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载