安卓 sqlite数据存入mysql_在Android上简单导出和导入SQLite数据库
這是一種將數(shù)據(jù)庫導(dǎo)出到名為備份文件夾的文件夾的簡單方法,您可以根據(jù)需要對(duì)其進(jìn)行命名,以及從同一文件夾導(dǎo)入數(shù)據(jù)庫的簡單方法public?class?ExportImportDB?extends?Activity?{
@Override
protected?void?onCreate(Bundle?savedInstanceState)?{
//?TODO?Auto-generated?method?stub
super.onCreate(savedInstanceState);//creating?a?new?folder?for?the?database?to?be?backuped?to
File?direct?=?new?File(Environment.getExternalStorageDirectory()?+?"/Exam?Creator");
if(!direct.exists())
{
if(direct.mkdir())
{
//directory?is?created;
}
}
exportDB();
importDB();
}
//importing?database
private?void?importDB()?{
//?TODO?Auto-generated?method?stub
try?{
File?sd?=?Environment.getExternalStorageDirectory();
File?data??=?Environment.getDataDirectory();
if?(sd.canWrite())?{
String??currentDBPath=?"//data//"?+?"PackageName"
+?"//databases//"?+?"DatabaseName";
String?backupDBPath??=?"/BackupFolder/DatabaseName";
File??backupDB=?new?File(data,?currentDBPath);
File?currentDB??=?new?File(sd,?backupDBPath);
FileChannel?src?=?new?FileInputStream(currentDB).getChannel();
FileChannel?dst?=?new?FileOutputStream(backupDB).getChannel();
dst.transferFrom(src,?0,?src.size());
src.close();
dst.close();
Toast.makeText(getBaseContext(),?backupDB.toString(),
Toast.LENGTH_LONG).show();
}
}?catch?(Exception?e)?{
Toast.makeText(getBaseContext(),?e.toString(),?Toast.LENGTH_LONG)
.show();
}
}
//exporting?database
private?void?exportDB()?{
//?TODO?Auto-generated?method?stub
try?{
File?sd?=?Environment.getExternalStorageDirectory();
File?data?=?Environment.getDataDirectory();
if?(sd.canWrite())?{
String??currentDBPath=?"//data//"?+?"PackageName"
+?"//databases//"?+?"DatabaseName";
String?backupDBPath??=?"/BackupFolder/DatabaseName";
File?currentDB?=?new?File(data,?currentDBPath);
File?backupDB?=?new?File(sd,?backupDBPath);
FileChannel?src?=?new?FileInputStream(currentDB).getChannel();
FileChannel?dst?=?new?FileOutputStream(backupDB).getChannel();
dst.transferFrom(src,?0,?src.size());
src.close();
dst.close();
Toast.makeText(getBaseContext(),?backupDB.toString(),
Toast.LENGTH_LONG).show();
}
}?catch?(Exception?e)?{
Toast.makeText(getBaseContext(),?e.toString(),?Toast.LENGTH_LONG)
.show();
}
}
}
別忘了添加此權(quán)限以繼續(xù)它
請(qǐng)享用
總結(jié)
以上是生活随笔為你收集整理的安卓 sqlite数据存入mysql_在Android上简单导出和导入SQLite数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql 数据库水平分表 存储过程
- 下一篇: Python内置函数简记