问题
I need to backup just some of the tables in my main database. The other tables are reference and are static so do not need to be backed up.
I have created a new blank DB that is on the SDCARD. Can I access the DB directly on the SDCARD or do I need to copy it when its finished backup?
The real question is can I iterate through the fields in each record in a loop or something so I dont have to have hundreds of line of code, one for each field.
In VB .NET I would do something like
For X = 0 to RS.Fields.Count
NewRS.Fields(x).value = Rs.Fields(x).value
etc... How wound I do that in android?
回答1:
I wrote a class to handle this. Yes my DB is at least 95% reference...
Here is the guts of the code:
Cursor c = DbBak.rawQuery(Sql, null);
String Cn[] = c.getColumnNames();
if (c != null ) {
if (c.moveToFirst()) {
do {
for ( x=0; x< c.getColumnCount(); x++)
{
newRow.put(Cn[x].toString(), c.getString(x));
}
Db.insert(TableName, null, newRow);
}while (c.moveToNext());
回答2:
Unless your reference tables make up 95% of your database size, I'd just copy the database file using standard Java file I/O, while the database is closed. That will be substantially faster than trying to schlep the data over cell-at-a-time.
来源:https://stackoverflow.com/questions/4697192/dynamic-database-backup-for-certain-tables