Question

I am trying to insert multiple records at once in sqlite database. I wanted to populate the DB and then populate the listview using that records. So I thought to execute the insert query in my second second activity, i.e., MenuActivity's oncreate method. I thought it would populate the DB and then I will be able to retrieve data as I require. But it is giving some error in logcat. Can anyone help on this.

MenuActivity.java

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_menu);

        //setting db environment
        dbhelper = new ToursOpenHelper(this);
        sqldb = dbhelper.getWritableDatabase();
        sqldb.execSQL("INSERT INTO "+tourEntry.TABLE+ 
" SELECT "+tourEntry.TOURTYPE+" AS 'Tour_type' , "+tourEntry.TOURNAME+" AS 'Tour_name', "+tourEntry.NIGHTS+" AS 'Nights', "+tourEntry.DAYS+" AS 'Day', "+tourEntry.PIC+" AS 'Pic'"+ 
" UNION SELECT '1', '3', 'Delhi Tour', '1', '2', 'img/mini-vacation/lotus.png'"+
" UNION SELECT '2', '3', 'Taj Mahal Tour By Train (Same Day Return)', '1', '1', 'img/mini-vacations/taj-train.jpg"+
" UNION SELECT '3', '3', 'Taj Mahal Trip By Car (Same Day Return)', '1', '1', 'img/mini-vacations/'"+
" UNION SELECT '4', '3', 'Taj Mahal Trip', '1', '2', 'img/mini-vacations/noimg.png'"+
" UNION SELECT '5', '3', 'Pink City Tour (Same Day Return)', '1', '1', 'img/mini-vacations/noimg.png'");

LogCat :

07-28 11:54:37.064: E/OpenGLRenderer(4396):   OpenGLRenderer is out of memory!
07-28 11:54:41.869: E/AndroidRuntime(4396): FATAL EXCEPTION: main
07-28 11:54:41.869: E/AndroidRuntime(4396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.travelplanner/com.example.travelplanner.MenuActivity}: android.database.sqlite.SQLiteException: near "3": syntax error: , while compiling: INSERT INTO tour SELECT tour_type AS 'Tour_type' , tour_name AS 'Tour_name', nights AS 'Nights', days AS 'Day', pic AS 'Pic' UNION SELECT '1', '3', 'Delhi Tour', '1', '2', 'img/mini-vacation/lotus.png' UNION SELECT '2', '3', 'Taj Mahal Tour By Train (Same Day Return)', '1', '1', 'img/mini-vacations/taj-train.jpg UNION SELECT '3', '3', 'Taj Mahal Trip By Car (Same Day Return)', '1', '1', 'img/mini-vacations/' UNION SELECT '4', '3', 'Taj Mahal Trip', '1', '2', 'img/mini-vacations/noimg.png' UNION SELECT '5', '3', 'Pink City Tour (Same Day Return)', '1', '1', 'img/mini-vacations/noimg.png'
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.os.Looper.loop(Looper.java:137)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.app.ActivityThread.main(ActivityThread.java:4448)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at java.lang.reflect.Method.invokeNative(Native Method)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at java.lang.reflect.Method.invoke(Method.java:511)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at dalvik.system.NativeStart.main(Native Method)
07-28 11:54:41.869: E/AndroidRuntime(4396): Caused by: android.database.sqlite.SQLiteException: near "3": syntax error: , while compiling: INSERT INTO tour SELECT tour_type AS 'Tour_type' , tour_name AS 'Tour_name', nights AS 'Nights', days AS 'Day', pic AS 'Pic' UNION SELECT '1', '3', 'Delhi Tour', '1', '2', 'img/mini-vacation/lotus.png' UNION SELECT '2', '3', 'Taj Mahal Tour By Train (Same Day Return)', '1', '1', 'img/mini-vacations/taj-train.jpg UNION SELECT '3', '3', 'Taj Mahal Trip By Car (Same Day Return)', '1', '1', 'img/mini-vacations/' UNION SELECT '4', '3', 'Taj Mahal Trip', '1', '2', 'img/mini-vacations/noimg.png' UNION SELECT '5', '3', 'Pink City Tour (Same Day Return)', '1', '1', 'img/mini-vacations/noimg.png'
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:266)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:88)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1905)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1845)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at com.example.travelplanner.MenuActivity.onCreate(MenuActivity.java:54)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.app.Activity.performCreate(Activity.java:4465)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-28 11:54:41.869: E/AndroidRuntime(4396):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
07-28 11:54:41.869: E/AndroidRuntime(4396):     ... 11 more
Was it helpful?

Solution

I wouldn't use do that in onCreate method as it only should be used in order to create database scheme and nothing else. The best approach would be creating another method which would do all insert operations in databaseAdapter or in totally new class and call it after I call onCreate method anywhere in application cycle.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top