简体   繁体   English

如何保留预定义的db文件并在Android中使用SQLiteDatabase类打开它?

[英]How to keep a predefined db file and open it with SQLiteDatabase class in android?

I am currently using SQLiteDatabase myDatabase=this.openOrCreateDatabase("conference",MODE_PRIVATE,null); 我目前正在使用SQLiteDatabase myDatabase=this.openOrCreateDatabase("conference",MODE_PRIVATE,null); for opening the database on the app side and myDatabase.execQuery() for executing queries. 用于在应用程序端打开数据库,以及用于执行查询的myDatabase.execQuery() Now when I was testing with different devices I found that when I installed the apk file on other phones, the database I prepared with execQuery() did not carry to other phones. 现在,当我在不同的设备上进行测试时,我发现当我在其他手机上安装apk文件时,使用execQuery()准备的数据库并没有带到其他手机上。

I am looking to solving this problem. 我正在寻找解决这个问题的方法。 I assume I must keep a already-prepared .db file somewhere in the raw folder and then open the database from there. 我假设必须在裸文件夹中的某个位置保留一个已经准备好的.db文件,然后从那里打开数据库。

How can I do that? 我怎样才能做到这一点?

You should try to implement Singleton class for application rather than database,because database is created in android you cannot add them as raw db 您应该尝试为应用而不是数据库实现Singleton类,因为数据库是在android中创建的,因此无法将其添加为原始数据库

example of singleton class 单例课程的例子

 public class CrimeLab {
    private static CrimeLab sCrimeLab;

    private List<Crime> mCrimes;
    public static CrimeLab get(Context context) {
        ...
    }
    private CrimeLab(Context context) {
        mCrimes = new ArrayList<>();
    }
    public List<Crime> getCrimes() {
        return mCrimes;
    }
    public Crime getCrime(UUID id) {
        for (Crime crime : mCrimes) {
            if (crime.getId().equals(id)) {
                return crime;
            }
        }
        return null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM