How to copy database file to sdcard in android

Go To StackoverFlow.com

1

Possible Duplicate:
How to backup database file to sdcard on android?

I'm working on phonegap 1.3.0. I've created database named test.db in phonegap which is stored in data/data/(pakage name)/test.db. I'm using native android code to copy the database to sdcard (mnt/sdcard). But it is giving error that test.db is not present. The device is not rooted. What I'm missing?

Thanks in advance.

Code is as follows->

public void copyfile(String srFile, String dtFile) throws FileNotFoundException{
    String DBPATH = "/data/data/package_name/databases/";
     String DBNAME = "test.db";
     String myPath = DBPATH + DBNAME;

    //Open your local db as the input stream
    File externalStorage = Environment.getExternalStorageDirectory();

    InputStream myInput = new FileInputStream(myPath);

    // Path to the just created empty db
    String outFileName = "/mnt/sdcard/folder/test.db";

    //Open the empty db as the output stream
    OutputStream myOutput;
    try {
    myOutput = new FileOutputStream(outFileName);
    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
    }
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}

The phonegap code is as follows:

<!DOCTYPE html>

<script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>


 <script type="text/javascript" charset="utf-8">
 db = window.openDatabase("test", "1.0", "PhoneGap Demo", 200000);
function createDB() {
    db.transaction(populateDB);
}
function populateDB(tx) {
    Field17=document.form.Field17.value;
    Field18=document.form.Field18.value;
    Field19=document.form.Field19.value;
    //tx.executeSql('DROP TABLE IF EXISTS INFO');
    tx.executeSql('CREATE TABLE IF NOT EXISTS INFO (pid integer primary key, field17,field18,field19');
    sql = "INSERT INTO INFO (field17,field18,field19) VALUES ( '" + Field17 + "','" + Field18 + "','" + Field19 + "')";
    tx.executeSql(sql);
    db.transaction(queryDB);
}


function queryDB(tx,results) {
    console.log("inside querydb");
    tx.executeSql('SELECT * FROM INFO', [], querySuccess);
}

function querySuccess(tx, results) {     
var len = results.rows.length;
    alert('rows inserted: ' + len);
    if (len > 0) {
        for (var i=0;i<len;i++) {

            console.log('pid: ' + results.rows.item(i).pid + 'field: ' + results.rows.item(i).field17 + ' field: ' + results.rows.item(i).field18+ ' field ' + results.rows.item(i).field19);

        }
    }
}       


</script>
2012-04-04 04:40
by neha ingale
new FileInputStream("myPath"); Is this line correct? or it should be new FileInputStream(myPath);Neeraj Nama 2012-04-04 05:06
Yeah right! sorry for mistake.. I have corrected it but still code is not working. I tried moving file from /mnt/sdcard to /mnt/sdcard/folder. It is working fine. But when I tried /data/data/pkg_name/databases/test.db to /mnt/sdcard/folder/test.db, it is not working - neha ingale 2012-04-04 06:20
Hi Please check code at dis link may be useful for you. http://stackoverflow.com/questions/2814213/making-a-database-backup-to-sdcard-on-androi - Manju 2012-04-04 10:50


1

you no need to root your phone. Check permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> .

you can check file

File dbFile = new File(DB_PATH + DB_NAME);
        if(dbFile.exists()){
                  //Continue

Edit: test this code. Change your pakage name and yourFoler in sdcard

public boolean checkDataBase() {
    File dbFile = new File(DB_PATH + DB_NAME);
    return dbFile.exists();
}

public void copyfile(String srFile, String dtFile) throws FileNotFoundException{
    private static String DB_NAME = "dictionary.db";
    private static String DB_PATH = "/data/data/com.app.android.dictionary/databases/";
if(checkDataBase()){
InputStream myInput = new FileInputStream(DB_PATH + DB_NAME);

    // Path to the just created empty db
    String outFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/foler/" + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput;
    try {
    myOutput = new FileOutputStream(outFileName);
    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
    }
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

}
}
2012-04-05 09:41
by judgement
I have mentioned WRITEEXTERNALSTORAGE permission in manifest. But it is not going in if(dbFile.exists()) condition. I'm getting error as test.db is not present - neha ingale 2012-04-05 12:52
trying to test edit code - judgement 2012-04-06 02:03
I tried the above code but checkDataBase() function is returning "false" - neha ingale 2012-04-06 11:40
Do you debug on Emulator or real phone?. You can use DDMS of Eclispe to check file test.db exists. File Explorer of DDMS check the path data/data/com.sun.(pakage name).../databases/ SQLite db fil - judgement 2012-04-07 01:32
No I'm using HTC wildfire S for testing - neha ingale 2012-04-09 04:22
i think your file doesn't exists. Check your file with USB debugging with DDMS. you can push or pull file from device - judgement 2012-04-09 06:55
Ok. But before running this code, I'm running my phonegap application so database should exists in data/data/pkgname/databases/ right - neha ingale 2012-04-09 07:18
so. Your phonegap application can't create database and store in data/data/pkg/database. Can you post phonegap app code?. I test successfull with ss galaxy s captivate. Rom Stock 2.1 & not roo - judgement 2012-04-09 08:47
I've added the Phonegap code in my question - neha ingale 2012-04-09 11:32
:). The error is path of db source file. Db file store in /data/data/pkg name/appdatabase/Database.db . The name of database is always Database.db. :). it is main database contains information of your database including sub folder and your database filename. Example: sub folder is file_0 and file name is 0000000000000001.db. (Sorry about my English grammar not good. :D - judgement 2012-04-10 02:09
Thanks! I tried using data/data/pkgname/Database.db as a path which should result into moving Database.db file to sdcard but it is not working. Also I tried data/data/pkgname/appdatabase/file_0/0000000000000001.db which is also not working. : - neha ingale 2012-04-10 04:38
check location of your create file. are you sure that file already exists?. You can push a db file example to test. And the location of me is /data/data/pkg name/app_database/Database.db - judgement 2012-04-10 04:51
From eclipse->DDMS->File explorer, I've seen the path. I couldn't push a database file to device as I don't have root permissions. Is there any other way to check whether the database file exists in the device or not - neha ingale 2012-04-10 05:01
why? i could push a database file to my device. Trying to up rom your device.no needed to root. (You can root and test with root explorer - judgement 2012-04-11 03:41


0

Application doesn't create the database, actually it is created by Android system and the file permission of the database is -rw-rw----. It means only the User and the group can access it and in this case both are system. So your program doesn't have right to access, hence you got the "Permission Denied" error.

2012-04-04 10:02
by Neeraj Nama
Thanks for the information. So to get the permission do I need to root the device - neha ingale 2012-04-04 12:31
Ads