gpt4 book ai didi

android - 在 Android 2.2 中复制 SQLite 数据库时出现问题

转载 作者:行者123 更新时间:2023-11-30 04:36:04 25 4
gpt4 key购买 nike

我必须设置 4.55Mb、2.2Mb 两个数据库。我们的目标是 2.2 版本。我尝试了一些方法,它设置了部分文件。(3096kb)。如果我们使用 API 10 那么它工作正常。如何才能我复制做什么?如何分块文件。

我这样试过:

private static String DB_PATH = "/data/data/com.android.xont.controller/databases/";
private static String DB_NAME = "";
private SQLiteDatabase ventura;
private final Context myContext;
// private SQLiteDatabase myDataBase=null;

private DataBaseHelper myDbHelper;

public DataBaseHelper(Context context, String DB_NAME) {
super(context, DB_NAME, null, 1);
this.myContext = context;
this.DB_NAME = DB_NAME;
}

/**
* Creates a empty database on the system and rewrites it with your own
* database.
**/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
File Path = myContext.getDir("Data", 0);
File DBFile = new File(Path, "DEMOUserVentura_HEMA.db");

if (dbExist) {
} else {
this.getReadableDatabase().close();
//this.getReadableDatabase().getPath();
//System.out.println( " ===== " + this.getReadableDatabase().getPath());
try {
copyDataBase();
} catch (IOException e) {
throw new Error(e);
}
}

}

private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}

if (checkDB != null) {
checkDB.close();
}

return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {
//AssetManager am = myContext.getAssets();
//String outFileName = DB_PATH + DB_NAME;
// OutputStream os = new FileOutputStream(outFileName);
//outFileName.createNewFile();
// byte []b = new byte[1024];
//int i, r;
//String []Files = am.list("");
// Arrays.sort(Files);
//for(i=1;i<10;i++)
//{
// String fn = String.format("%d.db", i);
//if(Arrays.binarySearch(Files, fn) < 0)
// break;
//InputStream is = am.open(fn);
// while((r = is.read(b)) != -1)
//os.write(b, 0, r);
//is.close();
// }
// os.close();

// 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 bytes from the inputfile to the 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;
ventura = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {
if (ventura != null)
ventura.close();
super.close();
}

请提供一些解决方案。请帮助我。

最佳答案

您看过 SQLite 备份 API 了吗:SQLite Backup API

它允许您使用 SQLite 本身以 block 的形式复制数据库文件,这可能会有所帮助。提供的链接包括一个简单的、有据可查的示例,尽管是用 C 语言编写的。

关于android - 在 Android 2.2 中复制 SQLite 数据库时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6829565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com