在一个项目中,我想从文件夹中复制sqlite db:
/ RES /原料
到文件夹:
像它在link中说的那样。并且它不会在目标文件夹上进行复制。这是我的代码: 的 MainActivity.java:/数据/数据/ {包名} /数据库/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Components onCreate","setContentView");
setContentView(R.layout.activity_main);
MySQLiteHelper n = new MySQLiteHelper(getApplicationContext());
try {
Uri url = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.myfile);
boolean temp = n.importDatabase(url.toString());
Log.i("Components onCreate",""+temp);
} catch (IOException e) {
e.printStackTrace();
}
MySQLiteHelper.java:
public boolean importDatabase(String dbPath) throws IOException {
// Close the SQLiteOpenHelper so it will commit the created empty
// database to internal storage.
close();
File newDb = new File(dbPath);
File oldDb = new File(DB_FILEPATH);
Log.i("ImportDatabase", "Database " + newDb.exists());
if (newDb.exists()) {
FileUtils.copyFile(new FileInputStream(newDb), new FileOutputStream(oldDb));
// Access the copied database so SQLiteHelper will cache it and mark
// it as created.
getWritableDatabase().close();
return true;
}
return false;
}
在我的logcat上,我一直都是假的。这是:
I/ImportDatabase﹕ Database false
我不知道自己错了什么?任何提示?感谢。
答案 0 :(得分:1)
将您的db文件放在assets文件夹中并使用此类:
package com.app.util;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Milad.
*/
public class DBHandler extends SQLiteOpenHelper {
private static String DB_PATH;
private SQLiteDatabase mDataBase;
private final Context mContext;
private static String DB_NAME = "myfile";// Database name
private static int DB_VERSION = 1;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DBHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
if (android.os.Build.VERSION.SDK_INT >= 4.2) {
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
} else {
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
this.mContext = context;
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
*/
public void createDataBase() throws IOException {
// If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if (!mDataBaseExist) {
this.getReadableDatabase();
this.close();
try {
// Copy the database from assests
copyDataBase();
Log.e("DataBaseHelper", "createDatabase database created");
} catch (IOException mIOException) {
throw new Error("Error Copying DataBase");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each
* time you open the application.
*
* @return true if it exists, false if it doesn't
*/
//Check that the database exists here: /data/data/your package/databases/Da Name
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
*/
private void copyDataBase() throws IOException {
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
public void openDataBase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
mDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if (mDataBase != null)
mDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
用法:
// dbHandler class instance
dbHandler = new DBHandler(this);
try {
dbHandler.createDataBase();
contentLog.append("Database Created\n");
} catch (IOException ioe) {
//throw new Error("Unable to create database");
}
答案 1 :(得分:0)