把android数据库复制到sd卡

public synchronized SQLiteDatabase getWritableDatabase() {
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
return mDatabase; // The database is already open for business


}


if (mIsInitializing) {
throw new IllegalStateException(
"getWritableDatabase called recursively");
}


// If we have a read-only database open, someone could be using it


// (though they shouldn't), which would cause a lock to be held on


// the file, and our attempts to open the database read-write would


// fail waiting for the file lock. To prevent that, we acquire the


// lock on the read-only database, which shuts out other users.


boolean success = false;
SQLiteDatabase db = null;
try {
mIsInitializing = true;
if (mName == null) {
db = SQLiteDatabase.create(null);

} else {

//得到sd卡ostocy/db/db文件的路径

String path = getDatabasePath(mName)
.getPath();

db = SQLiteDatabase.openOrCreateDatabase(path, null);
//InputStream myInput=getResources().openRawResource(R.raw.atcoffeejshopmactive);
InputStream myInput = null;
//读取assets目录下的db文件
 myInput = getClass().getResourceAsStream("/assets/db/atcoffeejshopmactive.db");


File fo=new File(path);
// Open the empty db as the output stream
OutputStream myOutput = null;
try {
myOutput = new FileOutputStream(path);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
try {
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

int version = db.getVersion();
if (version != mNewVersion) {
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
onUpgrade(db, version, mNewVersion);
}
db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}


onOpen(db);
success = true;
return db;
} finally {
mIsInitializing = false;
if (success) {
if (mDatabase != null) {
try {
mDatabase.close();
} catch (Exception e) {
}
}
mDatabase = db;
} else {
if (db != null)
db.close();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值