[Android] Database trong Android – P5 Backup and Import
A few days busy today against haphazard so new to the series written in Android Database. In part 5, also the last part I will guide you how to Backup and Import database in android.
Content
Step 1: Allow read and write data to the memory card in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cachhoc.net.tut.demodatabase" > <!-- set can read and write on sd card--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NoteActivity" android:label="@string/title_activity_note" > </activity> </application> </manifest>
Cập nhật file menu_main.xml – file menu của MainActivity
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/menu_more" android:icon="@drawable/ic_more" android:title="Menu" app:showAsAction="always"> <menu> <item android:id="@+id/menu_backup" android:orderInCategory="100" android:title="@string/backup_data" app:showAsAction="never" /> <item android:id="@+id/menu_import" android:orderInCategory="100" android:title="@string/import_data" app:showAsAction="never" /> </menu> </item> </menu>
You yourself have noted above 1 item – This item is a button 3 dot (overflow). When you click on the button 3 dots will appear 2 menu Backup và Import (watch video) is 2 items inside it.
MainActivity.java file updates to catch the events menu:
package cachhoc.net.tut.demodatabase; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener, BackupData.OnBackupListener { private ItemNoteAdapter adapter; private List<Note> listNote = new ArrayList<>(); private Context context; private DatabaseHelper db; private BackupData backupData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; db = new DatabaseHelper(context); backupData = new BackupData(context); backupData.setOnBackupListener(this); connectView(); } /** * connect java with xml view */ private void connectView() { // find Float Action Button findViewById(R.id.fab).setOnClickListener(this); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_note); // If the size of views will not change as the data changes. recyclerView.setHasFixedSize(true); // Setting the LayoutManager. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); // Setting the adapter. adapter = new ItemNoteAdapter(context, listNote); recyclerView.setAdapter(adapter); } /** * update list note when resume (open app or finish NoteActivity) */ public void onResume() { super.onResume(); updateListNote(); } /** * select all note from database and set to ls * use for loop to add into listNote. * We must add all item in ls into listNote then adapter can update * we add reverse ls to show new note at top of list */ private void updateListNote() { // clear old list listNote.clear(); // add all notes from database, reverse list ArrayList<Note> ls = db.getListNote("SELECT * FROM " + DatabaseHelper.TABLE_NOTE); // reverse list for (int i = ls.size() - 1; i >= 0; i--) { listNote.add(ls.get(i)); } adapter.notifyDataSetChanged(); } /** * display note have id */ public static void showNote(Context context, long id) { Intent intent = new Intent(context, NoteActivity.class); // send id to NoteActivity intent.putExtra(NoteActivity.ID, id); context.startActivity(intent); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.fab: showNote(context, NoteActivity.NEW_NOTE); break; default: break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_backup: backupData.exportToSD(); break; case R.id.menu_import: backupData.importFromSD(); break; default: break; } return super.onOptionsItemSelected(item); } @Override public void onFinishExport(String error) { String notify = error; if (error == null) { notify = "Export success"; } Toast.makeText(context, notify, Toast.LENGTH_SHORT).show(); } @Override public void onFinishImport(String error) { String notify = error; if (error == null) { notify = "Import success"; updateListNote(); } Toast.makeText(context, notify, Toast.LENGTH_SHORT).show(); } }
You note I have implement interface BackupData.OnBackupListener in class BackupData (in later would write) and override 2 method onFinishExport and onFinishImport its, 2 This method allows us to handle notification or after performing backup and import finished.
When starting your event call menu 2 method backupData.exportToSD and backupData.importFromSD to perform.
Step 3: Perform write Class BackupData
In her steps described earlier. When users choose backupdata then we will back up data to a memory card is located in a certain folder, in this alone it saved in folders MyNote. The program will automatically create this folder if it does not exist. The name of the backup file will be the name of the database together with date time, hours minutes seconds as the first video post.
When users select Import data, we will be asked if you want to back up your existing data before importing. If so, back up and then list the previous backup files to import. Otherwise the file to import only list only.
To avoid data loss and avoid errors in the case of existing data with new structures somewhat old data, We delete all records of existing data and then copy the backup file to a temporary database (temp database) and then copy the entire record in the table and temporary databse on current data. For example your current data table notes, This table column last_modified that in the old databse no, Meanwhile if copied directly leads to the database and do not have last_modified column faulty application.
Here is the code was relatively clear explanation. You can read.
package cachhoc.net.tut.demodatabase; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.AsyncTask; import android.os.Environment; import android.support.v4.app.FragmentActivity; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.text.SimpleDateFormat; import java.util.Date; public class BackupData { // url for database private final String dataPath = "//data//cachhoc.net.tut.demodatabase//databases//"; // name of main data private final String dataName = DatabaseHelper.DATABASE_NAME; // data main private final String data = dataPath + dataName; // name of temp data private final String dataTempName = DatabaseHelper.DATABASE_NAME + "_temp"; // temp data for copy data from sd then copy data temp into main data private final String dataTemp = dataPath + dataTempName; // folder on sd to backup data private final String folderSD = Environment.getExternalStorageDirectory() + "/MyNote"; private Context context; public BackupData(Context context) { this.context = context; } // create folder if it not exist private void createFolder() { File sd = new File(folderSD); if (!sd.exists()) { sd.mkdir(); System.out.println("create folder"); } else { System.out.println("exits"); } } /** * Copy database to sd card * name of file = database name + time when copy * When finish, we call onFinishExport method to send notify for activity */ public void exportToSD() { String error = null; try { createFolder(); File sd = new File(folderSD); if (sd.canWrite()) { SimpleDateFormat formatTime = new SimpleDateFormat("yyyy_MM_dd__HH_mm_ss"); String backupDBPath = dataName + "_" + formatTime.format(new Date()); File currentDB = new File(Environment.getDataDirectory(), data); File backupDB = new File(sd, backupDBPath); if (currentDB.exists()) { FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } else { System.out.println("db not exist"); } } } catch (Exception e) { e.printStackTrace(); error = "Error backup"; } onBackupListener.onFinishExport(error); } /** * import data from file backup on sd card * we must create a temp database for copy file on sd card to it. * Then we copy all row of temp database into main database. * It will keep struct of curren database not change when struct backup database is old * * @param fileNameOnSD name of file database backup on sd card */ public void importData(String fileNameOnSD) { File sd = new File(folderSD); // create temp database SQLiteDatabase dbBackup = context.openOrCreateDatabase(dataTempName, SQLiteDatabase.CREATE_IF_NECESSARY, null); String error = null; if (sd.canWrite()) { File currentDB = new File(Environment.getDataDirectory(), dataTemp); File backupDB = new File(sd, fileNameOnSD); if (currentDB.exists()) { FileChannel src; try { src = new FileInputStream(backupDB).getChannel(); FileChannel dst = new FileOutputStream(currentDB) .getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); } catch (FileNotFoundException e) { e.printStackTrace(); error = "Error load file"; } catch (IOException e) { error = "Error import"; } } } /** *when copy old database into temp database success * we copy all row of table into main database */ if (error == null) { new CopyDataAsyncTask(dbBackup).execute(); } else { onBackupListener.onFinishImport(error); } } /** * show dialog for select backup database before import database * if user select yes, we will export curren database * then show dialog to select old database to import * else we onoly show dialog to select old database to import */ public void importFromSD() { final AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AppCompatAlertDialogStyle); builder.setTitle(R.string.backup_data).setIcon(R.mipmap.ic_launcher) .setMessage(R.string.backup_before_import); builder.setPositiveButton(R.string.no, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showDialogListFile(folderSD); } }); builder.setNegativeButton(R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showDialogListFile(folderSD); exportToSD(); } }); builder.show(); } /** * show dialog list all backup file on sd card * @param forderPath folder conatain backup file */ private void showDialogListFile(String forderPath) { createFolder(); File forder = new File(forderPath); File[] listFile = forder.listFiles(); final String[] listFileName = new String[listFile.length]; for (int i = 0, j = listFile.length - 1; i < listFile.length; i++, j--) { listFileName[j] = listFile[i].getName(); } if (listFileName.length > 0) { // get layout for list LayoutInflater inflater = ((FragmentActivity) context).getLayoutInflater(); View convertView = (View) inflater.inflate(R.layout.list_backup_file, null); final AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AppCompatAlertDialogStyle); // set view for dialog builder.setView(convertView); builder.setTitle(R.string.select_file).setIcon(R.mipmap.ic_launcher); final AlertDialog alert = builder.create(); ListView lv = (ListView) convertView.findViewById(R.id.lv_backup); ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listFileName); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { alert.dismiss(); importData(listFileName[position]); } }); alert.show(); } else { final AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AppCompatAlertDialogStyle); builder.setTitle(R.string.delete).setIcon(R.mipmap.ic_launcher) .setMessage(R.string.backup_empty); builder.show(); } } /** * AsyncTask for copy data */ class CopyDataAsyncTask extends AsyncTask<Void, Void, Void> { ProgressDialog progress = new ProgressDialog(context); SQLiteDatabase db; public CopyDataAsyncTask(SQLiteDatabase dbBackup) { this.db = dbBackup; } /** * will call first */ @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); progress.setMessage("Importing..."); progress.show(); } @Override protected Void doInBackground(Void... params) { copyData(db); return null; } /** * end process */ @Override protected void onPostExecute(Void error) { // TODO Auto-generated method stub super.onPostExecute(error); if (progress.isShowing()) { progress.dismiss(); } onBackupListener.onFinishImport(null); } } /** * copy all row of temp database into main database * @param dbBackup */ private void copyData(SQLiteDatabase dbBackup) { DatabaseHelper db = new DatabaseHelper(context); db.deleteNote(null); /** copy all row of subject table */ Cursor cursor = dbBackup.query(true, DatabaseHelper.TABLE_NOTE, null, null, null, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Note note = db.cursorToNote(cursor); db.insertNote(note); cursor.moveToNext(); } cursor.close(); context.deleteDatabase(dataTempName); } private OnBackupListener onBackupListener; public void setOnBackupListener(OnBackupListener onBackupListener) { this.onBackupListener = onBackupListener; } public interface OnBackupListener { public void onFinishExport(String error); public void onFinishImport(String error); } }
You will need attention 2 following:
- Its use for importing database AsyncTask because this process can take a long time. It will help show 1 dialog to indicate the user is processed as if no user reassuring thought to have frozen applications.
-
During copy, I have called the command Note note = db.cursorToNote(cursor); cursorToNote previous order in class and the private DatabaseHelper, you now want to call the repair of public nhé.
The interface to import files list
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/lv_backup" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> </LinearLayout>
Some changes in the file string.xml
<resources> <string name="app_name">My Note</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="add">Add</string> <string name="delete">Delete</string> <string name="save">Save</string> <string name="yes">Yes</string> <string name="no">No</string> <string name="backup_data">Backup data</string> <string name="import_data">Import data</string> <string name="backup_before_import">Do you want backup curren database before import an old databas?</string> <string name="select_file">Select file</string> <string name="backup_empty">Backup file is empty</string> <!-- --> <string name="title_note">Title note</string> <string name="content">Content</string> <string name="title_activity_note">Create Note</string> </resources>
You can download the source code here.
That's it then. I wish you good learning.
Posts made in the tutorial Database trong Android by nguyenvanquan7826
Welcome Military,
First we found a series of Android your database very useful.
I've tried code according to your guide to the first part 5 Backup And Import. But I do not backup and import failed not, I have checked the code several times but could not find where the error.
Their code in eclipse, here are your source code. I hope you help her fix error. If you import the library project and then restart the project fails again until the error message is build is.
http://www.mediafire.com/download/jq14bp7dw4poe0a/SQLite_Demo.rar
Thanks Military!
Sorry too busy few days of this new test to help you get.
You meet 2 error:
– Write string table creation errors:
When you add KEY_LAST_MODIFIED_NOTE you do not have space in TEXT DEFAULT so the new installation will no more be record.
– Error backup because you wrong line:
You must copy from the table TABLE_NOTE not copy from the database.
Repair:
There is error for me in backupdata.java
In this line
SQLiteDatabase dbBackup = context.openOrCreateDatabase(dataTempName,
SQLiteDatabase.CREATE_IF_NECESSARY, null);
What is error? Can you post error here or take photo?
Hi,
Please can you update the gradle to android version 3. I am having too many problems with running the old program in the new version. I am a beginner so I don’t understand how to do the changes. I tried everything online, nothing helped.
Thank for report. This project in my tutorial use old version with you. Can you tell me your issue?
You could do an example backup and restore using Room persistence library? I’m having difficulty adapting this function to use Room Persistence library
private void copyData(SQLiteDatabase dbBackup) {
DatabaseHelper db = new DatabaseHelper(context);
db.deleteNote(null);
/** copy all row of subject table */
Cursor cursor = dbBackup.query(true, DatabaseHelper.TABLE_NOTE, null, null, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Note note = db.cursorToNote(cursor);
db.insertNote(note);
cursor.moveToNext();
}
cursor.close();
context.deleteDatabase(dataTempName);
}
Hi, Sorry about it, I do not use this library, so I do not know to make it.
Hi,
I am using your code to create a backup. Since getExternalStorageDirectory() has been deprecated. How to write the below .
private final String folderSD = Environment.getExternalStorageDirectory() + “/MyNote”;
Thanks,
Hi, you can see here: https://stackoverflow.com/questions/57116335/environment-getexternalstoragedirectory-deprecated-in-api-level-29-java