Perform ' in ' query operations in SQLite, how to match multiple values using Selectionargs

Source: Internet
Author: User

In Android development, SQLite is often used as a solution for local persistent storage. In ANDROIDSDK, has provided us with a variety of add, delete, change, check the API, although we can write our own SQL statement and then execute the Db.rawsql (sql,null) method, but there are some risks, that is, the stitching of the keyword may be illegal, This can cause a lot of unexpected and dangerous consequences. Therefore, it is recommended to use the API provided by the SDK for database operations, and Android has blocked this layer of risk for us.

when using SQLite queries, we often use the "in" operator to query, as

SELECT * FROM TableName where the UserID in (' Zhang San ', ' John Doe ', ' Harry ');
The above statement can be executed correctly, but as mentioned above, this method is unsafe. Therefore, we should use the Database.rawquery () method to execute. So, the question is: Learn the excavator technology, not, how should the parameters of this method be passed? Usually we will be splicing SQL when the use of this placeholder to denote a keyword, if in the following parentheses have multiple keywords, then there are a few key words we should pass a few? Go in. So you can use this method to stitch up the placeholder:

String makeplaceholders (int len) {        if (Len < 1) {            throw new RuntimeException ("No placeholders");        } else {
   stringbuilder sb = new StringBuilder (len * 2-1);            Sb.append ("?");            for (int i = 1; i < Len; i++) {                sb.append (",?");            }            return sb.tostring ();        }    }
You can do this when you are using:

String PlaceHolder = Makeplaceholders (Usernames.size ());                String sql = "SELECT DISTINCT username from" + TABLENAME + "where username in (" + PlaceHolder + ")";                String args = "";                for (int i = 0;i<usernames.size (); i++) {                    args + = Taguid.get (i). toString () + ",";                }                if (args. length>1) args = Args.substring (0,args. LENGTH-1);                cursor = db.rawquery (sql, Args.split (","));

OK, get it done ~

Perform ' in ' query operations in SQLite, how to match multiple values using Selectionargs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.