-1

我有一个 sq lite 数据库文件,其中填充了我想在我的应用程序中使用该数据的数据,之前也有人发布了这个问题,作为回复得到了这个链接:

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

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

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

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

4

2 回答 2

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) {

    }

}
于 2012-10-11T13:36:23.110 回答
0

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

于 2012-10-11T13:39:41.767 回答