0

我正在尝试在 sqlite 数据库中一次插入多条记录。我想填充数据库,然后使用该记录填充列表视图。所以我想在我的第二个活动中执行插入查询,即MenuActivity 的oncreate 方法。我认为它会填充数据库,然后我将能够根据需要检索数据。但它在 logcat 中给出了一些错误。任何人都可以帮助解决这个问题。

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'");

日志猫:

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
4

1 回答 1

0

我不会在 onCreate 方法中使用它,因为它只应该用于创建数据库方案而不是其他任何东西。最好的方法是创建另一个方法,该方法将在 databaseAdapter 或全新类中执行所有插入操作,并在我在应用程序周期的任何地方调用 onCreate 方法后调用它。

于 2013-07-27T11:26:08.157 回答