1

can we execute multiple statement like SQL Server store procedure with Android SQLite rawquery

Example:

string statement1 ="create temp table .....";

string statement2 ="insert into temptable select ..... table1"

string statement3="insert into temptable select..... table2"

string statement4="select ...... from temptable";

Cursor cur=SQLiteDatabase db = this.getReadableDatabase().rawQuery(statement1+" "+statement2+" "+statement3+" "+statement4, null);

and is there alternate way to achive this?

yivi
  • 42,438
  • 18
  • 116
  • 138
  • Have you seen this answer? https://stackoverflow.com/questions/32088056/how-to-bulk-insert-in-sqlite-in-android – Darush Apr 11 '18 at 19:42
  • Also this: https://stackoverflow.com/questions/2876236/efficient-batch-sql-query-execution-on-android-for-upgrade-database?rq=1 – Darush Apr 11 '18 at 19:42
  • Possible duplicate of [how to bulk insert in sqlite in android](https://stackoverflow.com/questions/32088056/how-to-bulk-insert-in-sqlite-in-android) – Darush Apr 11 '18 at 19:43
  • no i require calculation from multiple statement and prepare a result set – Meghraj Swami Apr 12 '18 at 18:26
  • If you carefully read the answers you will understand. start your batch query with db.beginTransaction();. then put all your statements before db.setTransactionSuccessful();. Finally db.endTransaction();. – Darush Apr 12 '18 at 18:31

1 Answers1

1
db.beginTransaction();
try {
    db.execSQL(statement1);
    db.execSQL(statement2);
    db.execSQL(statement3);
    Cursor cursor = db.query(temptable, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
    db.setTransactionSuccessful();
} finally {
    db.endTransaction();
}

At the end use the cursor to get your results.

Darush
  • 11,403
  • 9
  • 62
  • 60