由数据库浏览器创建的 sqlite 数据库在 android studio 中不起作用

sqlite db which created by db browser does not work in android studio

我使用 DB Browser 创建了一个 SQLite 数据库,当我将它移动到资产文件夹并在 android 工作室中打开它时,它看起来很糟糕并且无法运行。解决办法是什么 ? :(

这是一个screenshot

实际上,屏幕截图几乎可以确认资产文件夹中的文件是有效的 SQLite 数据库。

即匹配数据库头的前16个字节头字符串:"SQLite format 3[=74=]0" Database File Format.

Android Studio 本身不包含将文件作为 SQLite 数据库实际打开的要求(有工具,例如 DB Browser 可以执行此操作)。

您需要使用您的应用程序将文件作为 SQLite 数据库打开。这通常是通过将文件从资产文件夹复制到 suitable 位置(因为资产文件夹是包的一部分,它是只读的)然后通常通过 sub[=83 从该位置打开它来完成=] SQLiteOpenHelper.

有一个class可用,即SQLiteAssetHelper,可以使打开数据库相对容易。 注意 SQLiteAssetHelper 期望数据库(名称与文件名相同,包括扩展名)位于资产文件夹的 databases 文件夹中(您可能必须创建此文件夹,尽管屏幕截图显示您已经将数据库放入数据库文件夹中)。

例子

以下是使用 SQliteAssethelper 打开数据库的快速示例,在本例中根据 sqlite_master(SQlite 的大师 table).

Build.gradle (App) (其中一部分) :-

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' //<<<<<<<<<< ADDED JUST THIS LINE
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
  • 查看评论重新添加行

DatabaseHelper(SQLiteAssethelper 的子class)DBHelper.java

public class DBHelper extends SQLiteAssetHelper {

    public static final String DBNAME = "DaycareCenters.db";
    public static final int DBVERSION = 1;

    SQLiteDatabase mDB;

    public DBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        mDB = this.getWritableDatabase();
    }
}

调用 Activity(作为示例)MainActivity.java

public class MainActivity extends AppCompatActivity {

    DBHelper mDBHlpr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDBHlpr = new DBHelper(this);
        Cursor csr = mDBHlpr.mDB.rawQuery("SELECT * FROM sqlite_master",null);
        while (csr.moveToNext()) {
            Log.d("SQLITEMASTER","Name = " + csr.getString(csr.getColumnIndex("name")) + " Type = " + csr.getString(csr.getColumnIndex("type")));
        }
        csr.close();
    }
}

结果(写入日志)

  • 注意我只是复制了一个方便复制的数据库,tables等列表不会和你的一样

.

12-09 09:55:19.760 1473-1473/? I/SQLiteAssetHelper: successfully opened database DaycareCenters.db
12-09 09:55:19.764 1473-1473/? D/SQLITEMASTER: Name = android_metadata Type = table
12-09 09:55:19.764 1473-1473/? D/SQLITEMASTER: Name = player_card Type = table
12-09 09:55:19.764 1473-1473/? D/SQLITEMASTER: Name = sqlite_autoindex_player_card_1 Type = index
12-09 09:55:19.764 1473-1473/? D/SQLITEMASTER: Name = email Type = table
12-09 09:55:19.764 1473-1473/? D/SQLITEMASTER: Name = region Type = table
12-09 09:55:19.764 1473-1473/? D/SQLITEMASTER: Name = card Type = table
12-09 09:55:19.764 1473-1473/? D/SQLITEMASTER: Name = pc Type = table
12-09 09:55:19.764 1473-1473/? D/SQLITEMASTER: Name = sqlite_autoindex_pc_1 Type = index
  • 第一行来自 SQliteAssetHelper - 它确认数据库是从资产文件夹复制的。
  • 第 2 行显示 table android_metadata,这是将要添加的 android 特定 table,它包含语言环境。
  • 其他一些 tables.
  • 一些由于约束或隐含约束而自动创建的索引
  • 总之一切如期