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