查询db时强制关闭

时间:2013-11-28 18:50:02

标签: android

App使用外部创建的数据库(使用SQLitebrowser)。有代码将数据库从assets文件夹复制到系统位置。应用程序在Android 4.3,4.2,4.1的模拟器中运行良好。当我使用3.0和2.3在Emulator中查询数据库时,App Force关闭。 当我进入DDMS视图并检查data-> data-> package->数据库时,我只找到1个文件,所以我猜想不会创建或生成元数据。 我不确定3.0和2.3中不支持哪部分代码。 代码片段:

Button b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                name=et1.getText().toString();
                Intent i=new Intent(v.getContext(),Display.class);
                i.putExtra("dname",name);
                i.putExtra("dr", r);
                i.putExtra("dn",n);
                startActivity(i);

                }
            });

logcat的:

11-29 00:13:18.897: D/dalvikvm(328): GC_EXTERNAL_ALLOC freed 972 objects / 67008 bytes in 83ms
11-29 00:13:25.537: I/Database(328): sqlite returned: error code = 14, msg = cannot open file at source line 25467
11-29 00:13:25.537: E/Database(328): sqlite3_open_v2("/data/data/com.example.ast/databases/Astrology", &handle, 1, NULL) failed
11-29 00:13:25.677: I/Database(328): New database is being copied to device!
11-29 00:13:25.897: I/Database(328): New database has been copied to device!
11-29 00:13:25.917: I/Database(328): sqlite returned: error code = 1, msg = no such table: android_metadata
11-29 00:13:25.917: E/Database(328): SELECT locale FROM android_metadata failed
11-29 00:13:25.957: E/Database(328): Failed to setLocale() when constructing, closing the database
11-29 00:13:25.957: E/Database(328): android.database.sqlite.SQLiteException: no such table: android_metadata
11-29 00:13:25.957: E/Database(328):    at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
11-29 00:13:25.957: E/Database(328):    at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1950)
11-29 00:13:25.957: E/Database(328):    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1818)
11-29 00:13:25.957: E/Database(328):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
11-29 00:13:25.957: E/Database(328):    at com.example.ast.DataBaseHelper.checkDataBase(DataBaseHelper.java:68)
11-29 00:13:25.957: E/Database(328):    at com.example.ast.DataBaseHelper.openDataBase(DataBaseHelper.java:122)
11-29 00:13:25.957: E/Database(328):    at com.example.ast.Displayprediction.onCreate(Displayprediction.java:49)
11-29 00:13:25.957: E/Database(328):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 00:13:25.957: E/Database(328):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-29 00:13:25.957: E/Database(328):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-29 00:13:25.957: E/Database(328):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-29 00:13:25.957: E/Database(328):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-29 00:13:25.957: E/Database(328):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 00:13:25.957: E/Database(328):    at android.os.Looper.loop(Looper.java:123)
11-29 00:13:25.957: E/Database(328):    at android.app.ActivityThread.main(ActivityThread.java:4627)
11-29 00:13:25.957: E/Database(328):    at java.lang.reflect.Method.invokeNative(Native Method)
11-29 00:13:25.957: E/Database(328):    at java.lang.reflect.Method.invoke(Method.java:521)
11-29 00:13:25.957: E/Database(328):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-29 00:13:25.957: E/Database(328):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-29 00:13:25.957: E/Database(328):    at dalvik.system.NativeStart.main(Native Method)
11-29 00:13:25.957: I/Database(328): New database is being copied to device!
11-29 00:13:26.147: I/Database(328): New database has been copied to device!
11-29 00:13:26.147: D/AndroidRuntime(328): Shutting down VM
11-29 00:13:26.157: W/dalvikvm(328): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-29 00:13:26.167: E/AndroidRuntime(328): FATAL EXCEPTION: main
11-29 00:13:26.167: E/AndroidRuntime(328): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ast/com.example.ast.Displayprediction}: java.lang.NullPointerException
11-29 00:13:26.167: E/AndroidRuntime(328):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-29 00:13:26.167: E/AndroidRuntime(328):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-29 00:13:26.167: E/AndroidRuntime(328):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-29 00:13:26.167: E/AndroidRuntime(328):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-29 00:13:26.167: E/AndroidRuntime(328):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-29 00:13:26.167: E/AndroidRuntime(328):  at android.os.Looper.loop(Looper.java:123)
11-29 00:13:26.167: E/AndroidRuntime(328):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-29 00:13:26.167: E/AndroidRuntime(328):  at java.lang.reflect.Method.invokeNative(Native Method)
11-29 00:13:26.167: E/AndroidRuntime(328):  at java.lang.reflect.Method.invoke(Method.java:521)
11-29 00:13:26.167: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-29 00:13:26.167: E/AndroidRuntime(328):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-29 00:13:26.167: E/AndroidRuntime(328):  at dalvik.system.NativeStart.main(Native Method)
11-29 00:13:26.167: E/AndroidRuntime(328): Caused by: java.lang.NullPointerException
11-29 00:13:26.167: E/AndroidRuntime(328):  at com.example.ast.DataBaseHelper.getrPred(DataBaseHelper.java:208)
11-29 00:13:26.167: E/AndroidRuntime(328):  at com.example.ast.Displayprediction.onCreate(Displayprediction.java:106)
11-29 00:13:26.167: E/AndroidRuntime(328):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-29 00:13:26.167: E/AndroidRuntime(328):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-29 00:13:26.167: E/AndroidRuntime(328):  ... 11 more
11-29 00:13:30.357: I/Process(328): Sending signal. PID: 328 SIG: 9

1 个答案:

答案 0 :(得分:0)

这里有问题:

sqlite returned: error code = 1, msg = no such table: android_metadata

这正是它所说的:

no such table: android_metadata

好的,现在是解决问题的时候了: Android使用此表来处理db的一些元数据,即数据库的版本。如果您在PC(甚至是Mac)上创建了数据库文件,则没有特定于Android的表。我更喜欢从设备上的脚本创建数据库并用数据填充(CSV文件非常容易加载数据)。这是不可能的 - 只需在设备上创建一些数据库,将其从设备复制到PC上的某个数据库工具,您自己的表填充数据并在项目中使用。