Takuma 不上班
4 min readDec 27, 2016

【Android】Dump SQLite « 夢の回廊

又欠這篇很久了…… 主要還是來記錄一下要如何查看 Android SQLite 的欄位資料

下一篇是 Google Project exceed limit

不過還是先來寫這篇 寫一寫發現 Logdown 的編輯器真的有夠爛……

也該是時候換去 medium 了

剛好也是時候自己試著架網站了

正文

首先目前有兩種方法

  1. 最近看到的 Android Debug Database logdown一直刪我文字他喵的
  2. Dump (Export) SQLite Database

在這邊採用第二種方法,第一種還沒試過改天再來試試看

然後 Mac 使用者準備好一個工具 DB Browser for SQLite

首先我先拿我目前的 Project 來做範例

再來就是在自己程式碼裡加入以下這段 Code ,將 DB 儲存至 External Storage
記得要將 DATABASE_PATH + DATABASE_NAME 轉成自己原先 SQL 存放的位置

private static void backupDatabase() throws IOException {
//Open your local db as the input stream
String inFileName = DATABASE_PATH + DATABASE_NAME;;
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);

String outFileName = Environment.getExternalStoragePublicDirectory("/DMCollector/DMCollectorDB").getPath();
//Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
//Close the streams
output.flush();
output.close();
fis.close();
}

然後你就能在 External Storage 裡看到儲存的 DB 檔案

檔案出來了後其實也有很多方法可以將檔案拿出來
不過這邊還是用 Terminal 的方式快一點

adb pull mnt/sdcard/DMCollector/DMCollectorDB ~/Documents/work/Takuma/DM

然後就可以用上面說到的工具 DB Browser for SQLite 來開啟了 以下是欄位資料:

Originally published at takuma-nekomayoi-blog.logdown.com on December 27, 2016.