
1
方法一:登录SQL Server数据库管理器,选择目标数据库,右键菜单,点击【任务】——【分离】按钮,d出【分离数据库】窗体,在窗体中勾选中【删除连接】和【更新统计信息】复选框,然后点击【确定】按钮,即可完成数据库的分离。
2
按照上面的步骤对数据库进行分离之后,就可以对数据库进行复制、删除等 *** 作了。
3
方法二:选中【此电脑】,右键菜单,点击【管理】按钮,d出【计算机管理】窗体,在窗体中依次点击【服务和应用程序】——双击【服务】,打开计算机中的所有服务列表。
4
在打开的服务列表中找到SQL Server的相关服务:SQL Server(MSSQLSERVER),选中该服务,右键菜单,点击【停止】按钮,将该服务停止,这样就可以对数据库进行复制、删除等 *** 作了。
// 检查数据库是否有效private boolean checkDataBase() {
// SQLiteDatabase checkDB = null
// String myPath = DB_PATH + DB_NAME
File file = new File(DB_PATH, DB_NAME)
return file.exists()
}
public DBCtrl(Context context) {
super()
synchronized (lock) {
this.context = context
DB_PATH = context.getResources().getString(R.string.dbpath)
boolean dbExist = checkDataBase()
if (dbExist) {
// 数据库已存在,do nothing.
} else {
// 创建数据库
try {
File dir = new File(DB_PATH)
if (!dir.exists()) {
dir.mkdirs()
}
File dbf = new File(DB_PATH + DB_NAME)
if (dbf.exists()) {
dbf.delete()
}
// SQLiteDatabase.openOrCreateDatabase(dbf, null)
// 复制asseets中的db文件到DB_PATH下
copyDataBase()
} catch (IOException e) {
throw new Error("数据库创建失败")
}
}
}
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = context.getResources()
.openRawResource(R.raw.db)
// 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()
// }
}
我用的这个 构造 helper时候没有数据库就复制了
我觉得你失败的原因不是复制有问题而是数据太大了
android2.3以前不能直接读大于1m的资源的
你把数据库分割成小块复制过来时拼起来吧
法一:在企业管理器里找到数据库,右键,属性,可以看到数据库文件路径,然后停止服务管理器,就是右下角的绿色三角,然后去拷贝一份数据文件。然后在企业管理器里附加数据库,换一个名字里就可以了。这样就是2份一模一样的数据库。到时候程序里选择性的连接就OK了
法二。企业管理器,新建一个数据库,选择该数据库,右键,备份数据库。好了后选择还原数据库。要在选项卡的“在现有数据库上强制还原”复选框那里打勾
效果一样,但是要注意路径要是本身存在的,否则报错,(此方法无需停止服务管理器,但是 *** 作不细致容易出错)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)