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 Answers3

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});
Stan
  • 6,511
  • 8
  • 55
  • 87
  • How I can use : `%` in `cursor = sql.rawQuery("SELECT * FROM MyTable WHERE Comment in(?)", new String[]{params});` –  Apr 06 '15 at 10:57
  • for example :`SELECT tittle,parentID,_id FROM WebSite_CategoryBack WHERE tittle in ('%ACFD%','%SEESD%')` –  Apr 06 '15 at 10:59
  • Add `%` to the params builder code. And here, on SO, you have to accept answer if its really answers your question. – Stan Apr 06 '15 at 11:02
  • 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. –  Apr 06 '15 at 11:06
  • Do the same I did with the quotes. Looks like you are not familiar with Java at all... – Stan Apr 06 '15 at 11:10
  • I do same your response but symbol `%` don't work at statement 'IN' I need to use from `IN` same `LIKE` –  Apr 06 '15 at 11:14
  • Why are you trying to put % into `in` directly? What do you expect to get? `in (%'ACFD','SEESD')`? You have to enclose EVERY parameter you pass to `SQL` `IN` statement. `sb.append",".append('"').append('%').append(param).append('%').append('"');` ? – Stan Apr 06 '15 at 11:16
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.

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
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/

rubengees
  • 1,841
  • 2
  • 16
  • 31