Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

android - Copy SQLite database from assets folder

I wanna copy SQLite database from assets folder. This my DatabaseAdapter.java class

package com.example.dictionary;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseAdapter extends SQLiteOpenHelper {

    static String DB_PATH = "/data/data/com.example.dictionary/databases/";
    static String DB_NAME = "dict.db";
    SQLiteDatabase db;
    private final Context mContext;

    public DatabaseAdapter(Context context) {
        super(context, DB_NAME, null, 1);
        this.mContext = context;
    }

    public void createDB(){
        boolean dbExist = checkDB();
        if (dbExist) {

        } else {
            this.getReadableDatabase();

            try {
                copyDB();
            } catch (Exception e) {
                throw new Error("Error copying DB");

            }

        }
    }

    private void copyDB() throws IOException {
        InputStream dbInput = mContext.getAssets().open(DB_NAME);
        String outFile = DB_PATH + DB_NAME;
        OutputStream dbOutput = new FileOutputStream(outFile);

        byte[] buffer = new byte[1024];
        int length;
        while ((length = dbInput.read(buffer))>0) {
            dbOutput.write(buffer,0,length);
        }

        dbOutput.flush();
        dbOutput.close();
        dbInput.close();

    }

    private boolean checkDB() {
        SQLiteDatabase check = null;
        try {
            String dbPath = DB_PATH+DB_NAME;
            check = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY);
        } catch (Exception e) {
            // TODO: handle exception
        }
        if (check!=null) {
            check.close();
        }

        return check != null ? true : false;
    }

    public void openDB(){
        String dbPath = DB_PATH+DB_NAME;
        db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READONLY);
    }

    public synchronized void close(){
        if(db != null)
            db.close();
        super.close();
    }
    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    }

}

When i run app it isn't error. but when i check databases folder i saw file "dict.db" but only 12.00K and has only android_metadata table. Please help me. thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

put all this method in ur helper class.

public DataBaseHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
    mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

/**
 * This method will create database in application package /databases
 * directory when first time application launched
 **/
public void createDataBase() throws IOException {
    boolean mDataBaseExist = checkDataBase();
    if (!mDataBaseExist) {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException mIOException) {
            mIOException.printStackTrace();
            throw new Error("Error copying database");
        } finally {
            this.close();
        }
    }
}

/** This method checks whether database is exists or not **/
private boolean checkDataBase() {
    try {
        final String mPath = DATABASE_PATH + DATABASE_NAME;
        final File file = new File(mPath);
        if (file.exists())
            return true;
        else
            return false;
    } catch (SQLiteException e) {
        e.printStackTrace();
        return false;
    }
}

/**
 * This method will copy database from /assets directory to application
 * package /databases directory
 **/
private void copyDataBase() throws IOException {
    try {

        InputStream mInputStream = mContext.getAssets().open(DATABASE_NAME);
        String outFileName = DATABASE_PATH + DATABASE_NAME;
        OutputStream mOutputStream = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = mInputStream.read(buffer)) > 0) {
            mOutputStream.write(buffer, 0, length);
        }
        mOutputStream.flush();
        mOutputStream.close();
        mInputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/** This method open database for operations **/
public boolean openDataBase() throws SQLException {
    String mPath = DATABASE_PATH + DATABASE_NAME;
    myDataBase = SQLiteDatabase.openDatabase(mPath, null,
            SQLiteDatabase.OPEN_READWRITE);
    return myDataBase.isOpen();
}

/** This method close database connection and released occupied memory **/
@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    SQLiteDatabase.releaseMemory();
    super.close();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...