从资产文件夹中读取sqlite文件

Usm*_*iaz 20 android

我正在开发一个应用程序,我想sqlite从项目的资产文件夹中读取数据库文件.我搜索过网络但没有找到任何帮助.请帮我.谢谢.

GrI*_*sHu 12

您无法直接打开assets文件夹中的文件.相反,您需要将assets文件夹的sqlite数据库复制到内部/外部存储中,然后使用文件路径打开该文件.尝试下面的代码从assests中读取sqlite数据库并将其复制到sdcard中以使用它.

public class DataBaseHelper extends SQLiteOpenHelper {
   private Context mycontext;
   private static String DB_NAME = "(datbasename).sqlite";
   private static String DB_PATH ="/data/data/"+BuildConfig.APPLICATION_ID+"/databases/";
   public SQLiteDatabase myDataBase;

public DataBaseHelper(Context context) throws IOException {
    super(context,DB_NAME,null,1);
    this.mycontext=context;
    boolean dbexist = checkdatabase();
    if (dbexist) {
        System.out.println("Database exists");
        opendatabase(); 
    } else {
        System.out.println("Database doesn't exist");
        createdatabase();
    }
}

public void createdatabase() throws IOException {
    boolean dbexist = checkdatabase();
    if(dbexist) {
        System.out.println(" Database exists.");
    } else {
        this.getReadableDatabase();
        try {
            copydatabase();
        } catch(IOException e) {
            throw new Error("Error copying database");
        }
    }
}   

private boolean checkdatabase() {

    boolean checkdb = false;
    try {
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        checkdb = dbfile.exists();
    } catch(SQLiteException e) {
        System.out.println("Database doesn't exist");
    }
    return checkdb;
}

private void copydatabase() throws IOException {
    //Open your local db as the input stream
    InputStream myinput = mycontext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outfilename = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myoutput = new FileOutputStream(outfilename);

    // transfer byte to inputfile to outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myinput.read(buffer))>0) {
        myoutput.write(buffer,0,length);
    }

    //Close the streams
    myoutput.flush();
    myoutput.close();
    myinput.close();
}

public void opendatabase() throws SQLException {
    //Open the database
    String mypath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}

public synchronized void close() {
    if(myDataBase != null) {
        myDataBase.close();
    }
    super.close();
}
}
Run Code Online (Sandbox Code Playgroud)

  • 解决方案似乎是从http://stackoverflow.com/a/5949629/227556复制而没有归属. (6认同)

Com*_*are 10

使用SQLiteAssetHelper,其中包含首次运行应用程序时安装预打包数据库所需的所有代码.


A.J*_*uer 5

基本上,您可以获取文件名的输入流(从 AssetManager 打开函数)并将其写入输出流。

InputStream inputStream = getAssets().open(fileName);
Run Code Online (Sandbox Code Playgroud)

如果您使用 openOrCreateDatabase 创建数据库,您可能希望将数据库放在数据库文件夹中...

    String fileName = "MySQLiteDB.db";
    File file = getDatabasePath(fileName );
    if (!file.exists()) {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdir();
        }

        InputStream inputStream = getAssets().open(DATABASE_NAME);
        OutputStream outputStream = new FileOutputStream(file);
        byte[] buffer = new byte[1024 * 8];
        int numOfBytesToRead;
        while((numOfBytesToRead = inputStream.read(buffer)) > 0)
        outputStream.write(buffer, 0, numOfBytesToRead);
        inputStream.close();
        outputStream.close();
    }

    db = SQLiteDatabase.openOrCreateDatabase(file, null);
Run Code Online (Sandbox Code Playgroud)