如果您能帮助我,我将不胜感激。我的问题是:我的应用程序使用 2 个表创建数据库。我正在尝试备份此数据库并将其复制到 sd。这在模拟器上工作得很好,但是当涉及到真实设备时,文件被复制,但无法在任何 SQLite 浏览器中打开。
复制代码在这里
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = getDatabasePath("fieldworker").getAbsolutePath();
Toast.makeText(getApplicationContext(), currentDBPath, 2).show();
String backupDBPath = "fieldworker";
File currentDB = new File(currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
Toast.makeText(getApplicationContext(), Long.toString(src.size()), 2).show();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
else
{
Toast.makeText(getApplicationContext(), "Doesnt exist", 2).show();
}
}
} catch (Exception e) {
}
创建表的代码
public DBSqlLiteHelper(Context context)
{
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(TABLE_WORKERS_CREATE);
db.execSQL(TABLE_PERSONS_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
arg0.execSQL("DROP TABLE IF EXISTS " + TABLE_PERSONS);
arg0.execSQL("DsROP TABLE IF EXISTS " + TABLE_WORKERS);
onCreate(arg0);
}
Toast.makeText(getApplicationContext(), Long.toString(src.size())
这显示了数据库的大小,这意味着文件实际上是被复制的
谢谢吉姆