0

I want to execute a sqlite query, I have a String :

 String A = "AB-CDFER-GTDOL";
 String[] parts = Pattern.compile("-", Pattern.LITERAL).split(A);

Now I need to use from parts in my query.

cursor = sql.rawQuery("SELECT * FROM MyTable WHERE Comment in" + ?????, null);

How I can use from parts in my query.

For example I need to have ("AB","CDFER","GTDOL")

3 Answers 3

1

You can compile a string after split:

         String A = "AB-CDFER-GTDOL";
         String[] parts = Pattern.compile("-", Pattern.LITERAL).split(A);
    // build the params string
    StringBuilder sb = new StringBuilder("");
    for(String param:parts){
    // you can also enclose it in quotes
    sb.append",".append('"').append(param).append('"');
    }
    // remove 1st comma from sb.
    final String params = sb.toString().substr(1);

The trick here is to compile needed string as "AB","CDFER","GTDOL". Now you can pass this string as a ONE paramenter.

cursor = sql.rawQuery("SELECT * FROM MyTable WHERE Comment in(?)", new String[]{params});
Sign up to request clarification or add additional context in comments.

7 Comments

How I can use : % in cursor = sql.rawQuery("SELECT * FROM MyTable WHERE Comment in(?)", new String[]{params});
for example :SELECT tittle,parentID,_id FROM WebSite_CategoryBack WHERE tittle in ('%ACFD%','%SEESD%')
Add % to the params builder code. And here, on SO, you have to accept answer if its really answers your question.
In this query :SELECT tittle,parentID,_id FROM WebSite_CategoryBack WHERE tittle in ('%ACFD%','%SEESD%') I use from % but don't get me any result.
Do the same I did with the quotes. Looks like you are not familiar with Java at all...
|
0

I hope this will solve your problem:

You can split your string in the following way:

String A = "AB-CDFER-GTDOL";
 String[] parts = A.split("-");

Now your parts array will have the following values:

parts[0] = "AB";
parts[1] = "CDFER" etc..

Similarly you can use these value in your sql query as needed.

Hope this helps.

Comments

0

You can use the query Method from Android SQLDatabase. This is the Documentation for it: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query

You could implement it like the following:

 String A = "AB-CDFER-GTDOL";
 String[] parts = A.split("-");
 cursor = sql.query("MyTable", null, "Comment = ?", parts, null, null, null, null);

A helpful Article about SQL in Android: http://hmkcode.com/android-simple-sqlite-database-tutorial/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.