1. 程式人生 > >How to Correctly Use SQL's like in Android

How to Correctly Use SQL's like in Android

Yesterday I stumbled about the correct usage of the LIKE-statement in conjunction with selectionArgs.

My first attempt was to use it as this:


Cursor contactsContractContacts = 
    resolver.query(
    ContactsContract.Contacts.CONTENT_URI, projection, 
    ContactsContract.Contacts.DISPLAY_NAME + " like '%?%'" ,
    new String[]{filterStr}, 
    ContactsContract.Contacts.DISPLAY_NAME + " ASC");

Now this only led to this nice message in the log:


android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x13208a0

Hm. Maybe I should just drop the dashs?

But this also didn't work. The exception though has changed:


android.database.sqlite.SQLiteException: near "%": 
syntax error: , while compiling: 
SELECT ... FROM ... 
WHERE ((display_name like %?%)) 
ORDER BY display_name ASC

After searching around for an explanation I've finally found the solution on stackoverflow. Stackoverflow is a question and answer-platform for developers and has a very active Android-community. In this case SO-user ernazm and SO-user dalandroid described the solution which I adopted to my needs:


Cursor contactsContractContacts = resolver.query(
    ContactsContract.Contacts.CONTENT_URI, projection,
    ContactsContract.Contacts.DISPLAY_NAME + " like ?",
    new String[]{"%" + filterStr + "%"},
    ContactsContract.Contacts.DISPLAY_NAME + " ASC");

This finally worked! There is also an issue for this on Android's issue tracker, opened in 2009!

Things like that cost so much time - and can be pretty annoying. Gladly communities like stackoverflow exist to find solutions for such problems. Any stumbling block you want to comment about? What drove you to stackoverflow or Google for solutions?

相關推薦

How to Correctly Use SQL's like in Android

Yesterday I stumbled about the correct usage of the LIKE-statement in conjunction with selectionArgs. My first attemp

How to Correctly Store App-Specific Files in Android

Christophe Versieux (Waza_be) posted a rant about android developers' bad habit to store files directly on the root o

how to mount /system as read/write in android? 在除錯RK3288的OV2718的驅動時,需要remount /system目錄為rw以push檔案到/system/lib/hw目錄下,常規的是用adb登入上去後以root許可權執行mount -o re

在除錯RK3288的OV2718的驅動時,需要remount  /system目錄為rw以push檔案到/system/lib/hw目錄下,常規的是用adb登入上去後以root許可權執行mount -o remount,rw /system即可,然而在拿的新板子後這麼做失效了,於是百

How To Communicate Between Fragments and Activities in Android

Welcome to: how to communicate between fragments and activities in your android applications. If you have not done so already, please check out my previ

How to write tidy SQL queries in R

How to write tidy SQL queries in RMost of us have to interact with databases nowadays, and SQL is by far the most common language used. However, working wi

How to surprise your app’s users by hiding Easter eggs in the console

How to surprise your app’s users by hiding Easter eggs in the consoletoo much logging to the consoleI love console.logging(“stuff”).I do it throughout my a

java 讀取配置文件工具類 (how to read values from properties file in java)

讀取 public resource fault .get exce ram trac stat Java 讀取配置文件工具類 使用 java.util.Properties import java.io.IOException; import java.io.Inpu

How to create own operator with python in mxnet?

處理 需要 調用父類 rgs rop 數據類型 賦值 創建 recipe 繼承CustomOp 定義操作符,重寫前向後向方法,此時可以通過_init__ 方法傳遞需要用到的參數 1 class LossLayer(mxnet.operator.CustomOp):

【轉】How to initialize a two-dimensional array in Python?

use obj class amp example list tty address add 【wrong way:】 m=[[element] * numcols] * numrowsfor example: >>> m=[[‘a‘] *3] * 2&g

How to Get the Length of File in C

code class clas body position pre -c set == How to get length of file in C //=== int fileLen(FILE *fp) { int nRet = -1; int nPosB

在pycharm中以管理員身份運行/調試腳本(How to run / debug programs as root in Pycharm)

不想 http 設置 pan programs 額外 smi pytho 參考 如果想要在pycharm中以root的身份運行python腳本,因為pycharm本身好像沒有這個特性,目前只能通過一些額外的手段來實現。思路就是讓pycharm以root身份執行python編

Mysql Innodb 性能參數設置 https://www.rathishkumar.in/2017/01/how-to-allocate-innodb-buffer-pool-size-in-mysql.html

dea off variant sch 型號 pac san lin gin 參考原文: https://www.rathishkumar.in/2017/01/how-to-allocate-innodb-buffer-pool-size-in-mysql.html 查

How to setup a slave for replication in 6 simple steps with Percona XtraBackup

second path binlog ica direct isam fetch owin value Data is, by far, the most valuable part of a system. Having a backup done systema

[Tensorflow] 統計模型的引數數量 How to calculate the amount of parameters in my model?

import logging logging.basicConfig(level=logging.INFO, format='%(message)s', filemode='w', filename=config.logger) def _params_usage(): total

How to detect and extract forest areas in a aerial image map with the knowledge of DIP

Signal processing is a common subject in electrical engineering, communication engineering and mathematics that deals with analysis and processing

How To Load CSV Machine Learning Data in Weka (如何在Weka中載入CSV機器學習資料)

How To Load CSV Machine Learning Data in Weka 原文作者:Jason Brownlee 原文地址:https://machinelearningmastery.com/load-csv-machine-learning-data-weka/

How to Install and Configure OpenSSH Server In Linux

標題:在Linux中安裝和配置OpenSSH伺服器 Install OpenSSH in Linux  & 在Linux計算機中安裝OpenSSH   Being a network administrator requires a deep kno

How to Reset the root's Password for MySQL(MariaDB)

Unfortunately, I had forgotten the root's password of MySQL. Here, one of the methods is introduced, which works on my RasberryPi 3B+. K

[iOS] How to detect when iOS app appears in foreground, with Swift?

You can use the applicationDidBecomeActive(_:) method of your UIApplicationDelegate. You should read up on the app lifecycle. Your app delegate would then

[iOS] How to handle click event of BackBarButtonItem in nav bar?

滿神奇的,完全不用去拉 UI, 就這幾行程式,navigation bar 上面的按鈕就出來了,也可以bind 到事件。 我用的程式碼: UIImage *addImage = [UIImage imageNamed:@"plus2.png"]; UIBarButtonItem *newShareBu