简体   繁体   中英

how to store sql lite database in android studio to device memory?

I am new to android . I would like to store and retrieve records in android studio . I am using embedded sql database in android studio . Data are inserted perfectly when my device is connected to studio. whenever, i run after disconnecting my device insertion is not performing . what is the problem ? please help me, what should i need to do?

Try this code

  private void exportDB(){
  File sd = Environment.getExternalStorageDirectory();
  File data = Environment.getDataDirectory();
   FileChannel source=null;
   FileChannel destination=null;
   String currentDBPath = "/data/"+ "com.example.yourpackname" +"/databases/"+SAMPLE_DB_NAME;
   String backupDBPath = SAMPLE_DB_NAME;
   File currentDB = new File(data, currentDBPath);
   File backupDB = new File(sd, backupDBPath);
   try {
        source = new FileInputStream(currentDB).getChannel();
        destination = new FileOutputStream(backupDB).getChannel();
        destination.transferFrom(source, 0, source.size());
        source.close();
        destination.close();
        Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
    } catch(IOException e) {
        e.printStackTrace();
    }
}

Add permission in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

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