如何复制在我的应用程序中填充数据的sqlite数据库?

时间:2012-10-11 13:31:37

标签: android

我有一个sq lite数据库文件,填充了我希望在我的应用程序中使用该数据的数据,有人也早些时候发布了这个问题,回复得到了这个链接:

www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications /

问题是我将指定什么代替outFileName。

//打开空数据库作为输出流 OutputStream myOutput = new FileOutputStream(outFileName);

这个例子如何工作说我在资产文件夹中有文件abc.sqlite文件,如何使用它或首先创建一个新的db文件然后复制其内容?请帮忙

2 个答案:

答案 0 :(得分:0)

使用此代码解决您的问题

 /**
 * The Class DataBaseHelper.
 */
public class DataBaseHelper extends SQLiteOpenHelper {

    /** The D b_ path. */
    private final String DB_PATH = Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/folder/";

    /** The D b_ name. */
    private static String DB_NAME = "abc.db";

    /** The my data base. */
    private SQLiteDatabase myDataBase;

    /** The my context. */
    private final Context myContext;

    /**
     * Constructor Takes and keeps a reference of the passed context in order to
     * access to the application assets and resources.
     * 
     * @param context
     *            the context
     */
    public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        myContext = context;
        createDataBase();
    }

    /**
     * Creates a empty database on the system and rewrites it with your own
     * database.
     * */
    public void createDataBase() {

        boolean dbExist = checkDataBase();

        if (!dbExist) {
            getWritableDatabase();

            try {

                copyDataBase();

            } catch (IOException e) {

                throw new Error("Error copying database");

            }
        }
    }

    /**
     * Check if the database already exist to avoid re-copying the file each
     * time you open the application.
     * 
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase() {

        SQLiteDatabase checkDB = null;

        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);

        } catch (SQLiteException e) {

            e.printStackTrace();

        }

        if (checkDB != null) {

            checkDB.close();

        }

        return checkDB != null ? true : false;
    }

    /**
     * Copies your database from your local assets-folder to the just created
     * empty database in the system folder, from where it can be accessed and
     * handled. This is done by transfering bytestream.
     * 
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    private void copyDataBase() throws IOException {

        File f=new File(DB_PATH);
        f.mkdirs();
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        String outFileName = DB_PATH + DB_NAME;

        OutputStream myOutput = new FileOutputStream(outFileName);

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

        myOutput.flush();
        myOutput.close();
        myInput.close();

    }

    /**
     * Open data base.
     * 
     * @return the sQ lite database
     * @throws SQLException
     *             the sQL exception
     */
    public SQLiteDatabase openDataBase() throws SQLException {

        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
        return myDataBase;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.database.sqlite.SQLiteOpenHelper#close()
     */
    @Override
    public synchronized void close() {

        if (myDataBase != null)
            myDataBase.close();

        super.close();

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
     * .SQLiteDatabase)
     */
    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
     * .SQLiteDatabase, int, int)
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

}

答案 1 :(得分:0)

数据库文件进入应用程序的/ data /文件夹。这是在内部内存中,我不太确定,但我认为您可能需要root访问权限才能直接将其作为文件访问。