简体   繁体   中英

how import sqlite database android

When I export a database on sd card I use

try {
    File sd = new File(Environment.getExternalStorageDirectory()+"/ABC");
                    File data = Environment.getDataDirectory();
                    boolean success = false;
                    if (!sd.exists()) {
                        success = sd.mkdir();
                    }
                    if (sd.canWrite()) {
                        String currentDBPath = "\\data\\com.example.skurat\\databases\\Income.db";
                        String backupDBPath = "Income.db";
                        File currentDB = new File(data, currentDBPath);
                        File backupDB = new File(sd, backupDBPath);

                        if (currentDB.exists() && success) {
                            FileChannel src = new FileInputStream(currentDB).getChannel();
                            FileChannel dst = new FileOutputStream(backupDB).getChannel();
                            dst.transferFrom(src, 0, src.size());
                            src.close();
                            dst.close();
                        }
                    }
                } catch (Exception e) {
                }

as a result file "Income.db" is stored on the sd card.

When I import a new database from sd card I use

File dbfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/ABC/Income.db" ); 
             String backupDBPath = "Income.db";
             File backupDB = new File(dbfile, backupDBPath);
            SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
            // toDoDBAdapter = new DBAdapter(this, "Income.db"); written in the "onCreate" method, working correctly
            toDoDBAdapter.dbHelper.onUpgrade(db, 1, 2);
            // refresh screen, working correctly
            populateTodoList();

And database is not updated. Why? I use API 7

ps

If you know another way to import the database from sd card in the application, please tell, thanks

Your code is not clear. Do you actually use any subclass of SQLiteOpenHelper or not? If so the only thing you have to do is to make sure you provide proper target database version in helper constructor and the open helper will react properly running onUpgrade if current version number and target version number do not match.

There is no code that imports the database. Your code just calls onUpgrade on the database on the SD card.

You have to copy the database file back, like you do for export. Alternatively, you have to copy the records from that database into your normal database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM