Android:在姜饼之前从资产复制sqlite

Android:在姜饼之前从资产复制sqlite,android,database,sqlite,Android,Database,Sqlite,我在assets文件夹中有一个sqlite数据库,在第一次启动我的应用程序时,我将其复制到路径“data/data/databases” 这在姜饼的所有设备上都可以正常工作,但是对于较旧的版本,当我查询数据库时,我会得到一个TableNotFoundException。事实上,数据库中的db文件夹是空的。它只包含一个android_元数据表 下面是我用来复制数据库的两种方法: public static void copyDBInMemoryIfNeeded(Context ctx, Stri

我在assets文件夹中有一个sqlite数据库,在第一次启动我的应用程序时,我将其复制到路径“data/data/databases”

这在姜饼的所有设备上都可以正常工作,但是对于较旧的版本,当我查询数据库时,我会得到一个TableNotFoundException。事实上,数据库中的db文件夹是空的。它只包含一个android_元数据表

下面是我用来复制数据库的两种方法:

 public static void copyDBInMemoryIfNeeded(Context ctx, String pkgName) {
    try {
        String destPath = ctx.getFilesDir().getParentFile().getPath() + "/databases";
        File f = new File(destPath);
        if (!f.exists()) {
            f.mkdirs();
            f.createNewFile();
            copyDBInMemory(ctx.getAssets().open("mydb.sqlite"),
                    new FileOutputStream(destPath));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void copyDBInMemory(InputStream inputStream, OutputStream outputStream)
        throws IOException {
    // ---copy 1K bytes at a time---
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    inputStream.close();
    outputStream.close();
}
这里是我的DBAdapter课程:

public class DBAdapter {    

final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter(Context ctx) {
    this.context = ctx;
    DBHelper= new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion
                + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }
}

public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

public void close() {
    DBHelper.close();
}                                                                                              }

在Android 2.2和下面,在资产文件夹中使用SQLite文件时,应该考虑一些问题。超过1MB的文件将无法复制到您的应用程序专用数据库文件夹中。不确定这是错误还是其他原因,但如果您想使用数据库,可以在数据库帮助器中创建整个数据库架构


解决方法:不确定这是否是一个好的选择,但它在我使用12MB sqlite数据库的情况下有效,只需删除
.sqlite
.db
或文件名中的任何类型的数据库扩展名,它应该可以做到这一点。

我使用HJSplit取消了对数据库的拆分,然后将部件放在资产文件夹中。然后,我以这种方式将db复制到“data/data/databases”中:

    private static void copyDataBase(Context ctx, FileOutputStream os) throws IOException {
    AssetManager am = ctx.getAssets();
    byte[] b = new byte[1024];
    int r;
    for (int i = 1; i <= 5; i++) {
        InputStream is = am.open("mydb.sqlite.00" + i);
        while ((r = is.read(b)) != -1) {
            os.write(b, 0, r);
        }
        is.close();
    }
    os.close();
}
private static void copyDataBase(Context ctx,FileOutputStream os)引发IOException{
AssetManager am=ctx.getAssets();
字节[]b=新字节[1024];
INTR;

对于(int i=1;i)您的sql文件在assets文件夹中有多大?谢谢您的澄清!我尝试了您的解决方法,但不幸的是,它对我不起作用…我将尝试将我的db拆分为不同的文件,并将它们放入assets文件夹。如果您愿意,尝试放置一些其他文件扩展名,我曾经尝试使用.mp3,但它确实有效。