文章目錄
- 一、SQL檔案轉換以及導出
- 二、把sqlite檔案儲存到sd卡中使用
一、SQL檔案轉換以及導出
首先,我們需要用到一款炒雞好用的資料庫管理工具Navicat Premium 12 https://pan.baidu.com/s/1QPowcGITV13lmJ1NTxEkQw
(一)建立sqlite3連接配接
(二)建立一個sqlite3的資料庫檔案(記住檔案路徑)
(三)選擇資料傳輸
(四)把SQL的資料庫表傳輸到我們建立的sqlit檔案中
(五)選擇你要轉化的資料庫表
(六)等待資料傳輸結束
二、把sqlite檔案儲存到sd卡中使用
(一)把建立的sqlite3資料庫檔案儲存到Android res檔案下的raw檔案夾(自己建立)
(二)導入SQLdm類和MyDatabaseHelper類
/**
* 這個類就是實作從assets目錄讀取資料庫檔案然後寫入SDcard中,如果在SDcard中存在,就打開資料庫,不存在就從assets目錄下複制過去
* @author Big_Adamapple
*
*/
public class SQLdm {
//資料庫存儲路徑
String filePath = "data/data/包名/databases/xxx.db";
//資料庫存放的檔案夾
String pathStr = "data/data/包名/databases";
SQLiteDatabase database;
public SQLiteDatabase openDatabase(Context context){
System.out.println("filePath:"+filePath);
File jhPath=new File(filePath);
//檢視資料庫檔案是否存在
if(jhPath.exists()){
Log.i("test", "存在資料庫");
//存在則直接傳回打開的資料庫
return SQLiteDatabase.openOrCreateDatabase(jhPath, null);
}else{
//不存在先建立檔案夾
File path=new File(pathStr);
Log.i("test", "pathStr="+path);
if (path.mkdir()){
Log.i("test", "建立成功");
}else{
Log.i("test", "建立失敗");
};
try {
//得到資源
AssetManager am= context.getAssets();
//得到資料庫的輸入流
InputStream is=am.open("c.db");
Log.i("test", is+"");
//用輸出流寫到SDcard上面
FileOutputStream fos=new FileOutputStream(jhPath);
Log.i("test", "fos="+fos);
Log.i("test", "jhPath="+jhPath);
//建立byte數組 用于1KB寫一次
byte[] buffer=new byte[1024];
int count = 0;
while((count = is.read(buffer))>0){
Log.i("test", "得到");
fos.write(buffer,0,count);
}
//最後關閉就可以了
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
//如果沒有這個資料庫 我們已經把他寫到SD卡上了,然後在執行一次這個方法 就可以傳回資料庫了
return openDatabase(context);
}
}
}
public class MyDatabaseHelper {
private final int BUFFER_SIZE = 400000;
public static final String DB_NAME = "xxxxxx.db"; //儲存的資料庫檔案名
public static final String PACKAGE_NAME = "xxxxxxxx";//程式的包名
public static final String DB_PATH = "/data"
+ Environment.getDataDirectory().getAbsolutePath() + "/"
+ PACKAGE_NAME + "/databases"; //sd卡存放資料庫的位置
private SQLiteDatabase database;
private Context context;
public MyDatabaseHelper(Context context){
this.context = context;
}
public void openDatabase() {
File dFile=new File(DB_PATH);//判斷路徑是否存在,不存在則建立路徑
if (!dFile.exists()) {
dFile.mkdir();
}
this.database = this.openDatabase(DB_PATH + "/" + DB_NAME);
}
private SQLiteDatabase openDatabase(String dbfile) {
try {
if (!(new File(dbfile).exists())) {
InputStream is = this.context.getResources().openRawResource(
//todo 欲導入的資料庫
R.raw.xxxx);
FileOutputStream fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
null);
return db;
}catch (FileNotFoundException e) {
Log.e("Database", "File not found");
e.printStackTrace();
} catch (IOException e) {
Log.e("Database", "IO exception");
e.printStackTrace();
}
return null;
}
public void closeDatabase() {
this.database.close();
}
}
使用:
//把資料庫寫入sd卡
myDatabaseHelper = new MyDatabaseHelper(getActivity());
myDatabaseHelper.openDatabase();
myDatabaseHelper.closeDatabase();
//讀取sd内的資料庫
SQLdm s = new SQLdm();
SQLiteDatabase database = s.openDatabase(this.getContext());
Cursor cursor = database.rawQuery(sql語句, null);
while (cursor.moveToNext()) {
//根據列号得到資料
String id = cursor.getString(0);
…………
list.add(new intangible_heritage_Account(id,xxxx));
}
cursor.close();
database.close();