如何备份和恢复我的 SQLite 数据库数据到 firebase
我的数据库帮助:
公共类 DatabaseHelper 扩展 SQLiteOpenHelper {
public static final String DATABASE_NAME = "user_details.db";
/* User Details Table */
public static final String TABLE_NAME_1 = "user_details";
public static final String COL_1_1 = "ID";
public static final String COL_2_1 = "PHONE_NUMBER";
public static final String COL_3_1 = "NAME";
public static final String COL_4_1 = "PASSCODE";
public static final String COL_5_1 = "BUSSINESS_NAME";
public static final String COL_6_1 = "LOCATION";
public static final String COL_7_1 = "CREATED_TIME";
public static final String COL_8_1 = "STATUS"; // 1] Personal 2] Add Contact 3] Delete
public static final String COL_9_1 = "IMAGE";
/* Transaction Table */
......
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME_1 +" ("+COL_1_1+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COL_2_1+" INTEGER,"+COL_3_1+" TEXT,"+COL_4_1+" TEXT,"+COL_5_1+" TEXT,"+COL_6_1+" TEXT,"+COL_7_1+" DEFAULT CURRENT_TIMESTAMP,"+COL_8_1+" INTEGER,"+COL_9_1+" blob)");
db.execSQL("create table " + TABLE_NAME_2 +" ("+COL_1_2+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COL_2_2+" INTEGER,"+COL_3_2+" INTEGER,"+COL_4_2+" INTEGER,"+COL_5_2+" INTEGER,"+COL_6_2+" TEXT,"+COL_7_2+" DEFAULT CURRENT_TIMESTAMP,"+COL_8_2+" TEXT)");
db.execSQL("create table " + TABLE_NAME_3 +" ("+COL_1_3+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COL_2_3+" INTEGER,"+COL_3_3+" INTEGER,"+COL_4_3+" DEFAULT CURRENT_TIMESTAMP)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME_1);
我的数据库帮助:
公共类 DatabaseHelper 扩展 SQLiteOpenHelper {
public static final String DATABASE_NAME = "user_details.db";
/* User Details Table */
public static final String TABLE_NAME_1 = "user_details";
public static final String COL_1_1 = "ID";
public static final String COL_2_1 = "PHONE_NUMBER";
public static final String COL_3_1 = "NAME";
public static final String COL_4_1 = "PASSCODE";
public static final String COL_5_1 = "BUSSINESS_NAME";
public static final String COL_6_1 = "LOCATION";
public static final String COL_7_1 = "CREATED_TIME";
public static final String COL_8_1 = "STATUS"; // 1] Personal 2] Add Contact 3] Delete
public static final String COL_9_1 = "IMAGE";
/* Transaction Table */
......
@Override
public void onCreate(SQLiteDatabase
还有我的仪表板.java...
以下是将 SQLite 数据库备份和恢复到 Firebase 的步骤:
将 SQLite 备份到文件:
// Get the databases path
String dbPath = context.getDatabasePath(DatabaseHelper.DATABASE_NAME).getPath();
// Backup database to sdcard
File backupFile = new File(Environment.getExternalStorageDirectory(), DatabaseHelper.DATABASE_NAME);
// Open file for writing
FileOutputStream outputStream = new FileOutputStream(backupFile);
// Open the database
SQLiteDatabase db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY);
// Copy database content to output stream
OutputStream output = new BufferedOutputStream(outputStream);
db.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME_1, null).moveToFirst();
while (!db.isAfterLast()) {
output.write(db.getInt(0));
db.moveToNext();
}
output.flush();
output.close();
db.close();
将备份文件上传到 Firebase Storage
// Get reference to storage
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
// Upload file to path
storageRef.child("backups").child(backupFile.getName()).putFile(Uri.fromFile(backupFile));
从 Firebase 存储下载备份并恢复:
// Get reference to backup file
StorageReference backupRef = storageRef.child("backups").child(backupFile.getName());
File restoreFile = new File(context.getCacheDir(), backupFile.getName());
// Download to local cache
backupRef.getFile(restoreFile);
// Open SQLite DB
SQLiteDatabase db = context.openOrCreateDatabase(DatabaseHelper.DATABASE_NAME, 0, null);
// Restore data from file
db.beginTransaction();
db.execSQL("DELETE FROM " + DatabaseHelper.TABLE_NAME_1);
FileInputStream input = new FileInputStream(restoreFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String line = reader.readLine();
while (line != null) {
// Parse data and insert
db.execSQL(...);
line = reader.readLine();
}
db.setTransactionSuccessful();
db.endTransaction();
reader.close();
input.close();