如何导入SQL数据库文件

时间:2017-09-02 22:35:43

标签: android database sqlite

我正在按照this建议的指南创建一个带有预加载数据的SQLite数据库;但是我无法理解如何生成所需的数据库文件(.db)。

我使用了“DB Browser for SQLite”软件来编写我的数据库,但似乎只能将文件保存为创建数据库的命令(CREATE TABLE IF NOT EXISTS等。)。

如何将文件保存为所需的.db格式?

This网站的解决方案是我认为我的文件格式错误的原因。

对于上下文,我遇到的错误如下:

09-02 23:47:58.681 19704-19704/company.app W/SQLiteAssetHelper: copying database from assets...
09-02 23:47:58.682 19704-19704/company.app W/SQLiteAssetHelper: database copy complete
09-02 23:47:58.684 19704-19704/company.app E/SQLiteLog: (26) file is encrypted or is not a database
09-02 23:47:58.684 19704-19704/company.app E/DefaultDatabaseErrorHandler: Corruption reported by sqlite on database: /data/user/0/company.app /databases/app .db
09-02 23:47:58.684 19704-19704/company.app E/DefaultDatabaseErrorHandler: deleting the database file: /data/user/0/company.app /databases/app .db
09-02 23:47:58.686 19704-19704/company.app E/SQLiteLog: (14) cannot open file at line 32504 of [bda77dda96]
09-02 23:47:58.686 19704-19704/company.app E/SQLiteLog: (14) os_unix.c:32504: (2) open(/data/user/0/company.app /databases/app .db) - 
09-02 23:47:58.687 19704-19704/company.app E/SQLiteDatabase: Failed to open database '/data/user/0/company.app /databases/app .db'.
                                                                        android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
                                                                            at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
                                                                            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
                                                                            at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
                                                                            at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
                                                                            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
                                                                            at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
                                                                            at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
                                                                            at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:796)
                                                                            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
                                                                            at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
                                                                            at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.returnDatabase(SQLiteAssetHelper.java:408)
                                                                            at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.createOrOpenDatabase(SQLiteAssetHelper.java:401)
                                                                            at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:176)
                                                                            at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
                                                                            at company.app .MyDatabase.randomiseItBro(MyDatabase.java:33)
                                                                            at company.app .MainActivity.randomise(MainActivity.java:55)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at android.view.View$DeclaredOnClickListener.onClick(View.java:4695)
                                                                            at android.view.View.performClick(View.java:5612)
                                                                            at android.view.View$PerformClick.run(View.java:22285)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
09-02 23:47:58.688 19704-19704/company.app W/SQLiteAssetHelper: could not open database app .db - unknown error (code 14): Could not open database
09-02 23:47:58.689 19704-19704/company.app D/AndroidRuntime: Shutting down VM
09-02 23:47:58.689 19704-19704/company.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: company.app , PID: 19704
                                                                        java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                            at android.view.View$DeclaredOnClickListener.onClick(View.java:4700)
                                                                            at android.view.View.performClick(View.java:5612)
                                                                            at android.view.View$PerformClick.run(View.java:22285)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                                                                         Caused by: java.lang.reflect.InvocationTargetException
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at android.view.View$DeclaredOnClickListener.onClick(View.java:4695)
                                                                            at android.view.View.performClick(View.java:5612) 
                                                                            at android.view.View$PerformClick.run(View.java:22285) 
                                                                            at android.os.Handler.handleCallback(Handler.java:751) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                            at android.os.Looper.loop(Looper.java:154) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6123) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 
                                                                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
                                                                            at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:178)
                                                                            at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
                                                                            at company.app .MyDatabase.randomiseItBro(MyDatabase.java:33)
                                                                            at company.app .MainActivity.randomise(MainActivity.java:55)
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at android.view.View$DeclaredOnClickListener.onClick(View.java:4695) 
                                                                            at android.view.View.performClick(View.java:5612) 
                                                                            at android.view.View$PerformClick.run(View.java:22285) 
                                                                            at android.os.Handler.handleCallback(Handler.java:751) 
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                            at android.os.Looper.loop(Looper.java:154) 
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6123) 
                                                                            at java.lang.reflect.Method.invoke(Native Method) 
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

3 个答案:

答案 0 :(得分:1)

单击“新建数据库”工具栏按钮时,将创建SQLite数据库文件。创建表和内容时,单击“写入更改”工具栏按钮,即可更新该数据库文件。获得所需数据库文件后,即assets/中包含的文件,供SQLiteAssetHelper使用。

对于SQLite的DB Browser的Linux版本默认情况下不会将文件扩展名应用于该文件。您需要在创建数据库文件时自己添加.db,或在创建数据库文件后重命名它。

答案 1 :(得分:0)

您可以像myfunc.js文件或index.html一样创建一个文件,但这个文件是带代码的mydb.sql。常规word文件ex:

CREATE DATABASE activedb; CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... );

然后你可以使用database_name < mydb.sql 我认为这就是你想要做的。我不确定。

答案 2 :(得分:0)

如果您在应用程序中正确地将数据库从资产转移到数据库目录,那么您可能会错过以下两个要求之一:

  1. 主键需要命名为_id
  2. 数据库需要一个您需要的特定表:CREATE TABLE android_metadata (_id INTEGER PRIMARY KEY, locale TEXT)