如何在android studio中将sql lite数据库存储到设备内存?

时间:2015-08-11 05:43:59

标签: java android sqlite android-studio

我是android新手。我想在android studio中存储和检索记录。我在android studio中使用嵌入式sql数据库。当我的设备连接到工作室时,数据会完美插入。无论什么时候,我在断开设备插入后运行都没有执行。问题是什么 ?请帮帮我,我该怎么办?

1 个答案:

答案 0 :(得分:0)

试试此代码

  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();
    }
}

在清单中添加权限

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