1. 程式人生 > >Android 資料庫轉儲

Android 資料庫轉儲

先建一個工具類MigrateDBUtil

package com.example.myapplication2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author a-yuexia
 * @data 2018-12-7
 */
public class MigrateDBUtil { private static final String TAG = "MigrateDBUtil"; private static Context sContext; public static void migrateDB(Context context) { sContext = context; migrate(); deleteOldDB(); } /** * 判斷data/data/packageName/database內是否存在kuaiyun.db, * 不存在,開始遷移. */
private static void migrate() { //你在轉儲之前要先建立一個數據庫這樣才有databases這個資料夾,不然會報空指標 File file = new File("/data/data/com.example.myapplication2/databases/kuaiyu.db"); if (!file.exists()) { FileOutputStream writer = null; InputStream reader = null; try {
Log.d(TAG, "1: "); reader = sContext.getAssets().open("station.db"); Log.d(TAG, "2: "); file.createNewFile(); writer = new FileOutputStream(file); byte[] bytes = new byte[1024]; int i; while ((i = reader.read(bytes)) != -1) { writer.write(bytes, 0, i); } Log.d(TAG, "3: "); } catch (Exception e) { e.printStackTrace(); } finally { try { if (writer != null) { writer.close(); } if (reader != null) { reader.close(); } } catch (IOException e) { } } } } /** * 初始化資料庫 * 遷移資料庫 Version為1. */ public static SQLiteDatabase initDatabase(Context context) { return DataBaseHelper.getInstance(context).getReadableDatabase(); } private static void deleteOldDB() { sContext.deleteFile("station.db"); } }
station.db存放的位置和資料夾名稱已及注意轉儲資料庫的版本

像我的station.db是24那麼我轉儲之後理性的就是25.總之不能比24低
在這裡插入圖片描述

		//建立個數據庫生成databases資料夾
        DataBase dbHelper = new DataBase(MainActivity.this, "test_db",null,1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        //然後轉儲資料庫
        MigrateDBUtil.migrateDB(MainActivity.this);
強調一下啊,你現在是有兩個資料庫,用哪個資料庫的表就new哪個資料庫注意版本version別返回主選單就從2變成1,這不符合邏輯,
		DataBase dbHelper = new DataBase(StartActivity.this, "test_db",null,1);
        db = dbHelper.getWritableDatabase();
        //轉存資料庫引用
        dbstart=MigrateDBUtil.initDatabase(this);

DateBaseHelper

package com.example.myapplication2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by a-yuexia on 2018/12/7.
 */

public class DataBaseHelper extends SQLiteOpenHelper{
    public final static String name = "kuaiyu.db";
    public static final int version = 25;
    private static DataBaseHelper dataBaseHelper = null;

    public DataBaseHelper (Context context) {
        super(context, name, null, version);

    }
    public static DataBaseHelper getInstance(Context context) {
        if (dataBaseHelper == null) {
            dataBaseHelper = new DataBaseHelper (context);
        }
        return  dataBaseHelper ;
    }

    public DataBaseHelper (Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


}

小編忘記改類名自己弄一下

DateBase

package com.example.myapplication2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by a-yuexia on 2018/12/7.
 */

public class DataBase extends SQLiteOpenHelper {
    public DataBase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String stu_table="create table startinfo(id integer primary key autoincrement,name text)";
        db.execSQL(stu_table);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

好了資料庫轉儲結束了
有什麼疑問或者意見都可以聯絡我
QQ:2714730493