我自己的目录中的Android数据库

时间:2012-03-22 17:08:19

标签: android database sqlite directory

我正在尝试在Android应用程序中使用自己的数据库。大多数关于此主题的主题和教程都建议您创建它并使用SQLiteOpenHelper打开它,默认情况下使用data/data/com.my.application/databases文件夹。我已经看到了将文件从context.getAssets.open(dbname)复制到默认sqlite路径的建议。如果这实际上是解决方案,那么传递我的database.db文件的正确途径是什么?

1 个答案:

答案 0 :(得分:0)

我的朋友,你可以试试这个代码

/**
 * 
 * Purpose: Open and Read database to fetch records from different database
 * tables.
 * 
 * @version 1.0
 * 
 * @author ketan kalariya(ketan.kalariya@indianic.com) 
 *        
 * **/

    public class DataBaseHelper extends SQLiteOpenHelper {

        private Context myContext;
        private SQLiteDatabase myDataBase;
        // Comman KEYs of tables
        public static String MASTERFENCE_TABLE = "MasterFence";
        public static String KEY_ID = "_id";
        public static String AREA = "area";
        public static String TAG = "tag";
        public static String TITLE = "title";
        public static String DESCR = "description";
        public static String NO = "no";

        public static String ADDFENCEPOINT_TABLE = "AddFencePoint";
        public static String ADDLATLONG_TABLE = "AddLatLong";
        public static String LAT = "lat";
        public static String LONG = "long";

        public static String TEMPFENCEPOINT_TABLE = "TempFencePoint";
        public static String TEMPLATLONG_TABLE = "TempLatLong";

        public DataBaseHelper(Context context) {
            super(context, context.getResources().getString(R.string.dbname), null,
                    1);
            this.myContext = context;
        }

        @Override
        public void onCreate(SQLiteDatabase arg0) {

        }

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

        // ---Create the database---
        public void createDataBase() throws IOException {

            // ---Check whether database is already created or not---
            boolean dbExist = checkDataBase();

            if (!dbExist) {
                this.getReadableDatabase();
                try {
                    // ---If not created then copy the database---
                    copyDataBase();
                    this.close();
                } catch (IOException e) {
                    throw new Error("Error copying database");
                }
            }
        }

        // --- Check whether database already created or not---
        private boolean checkDataBase() {
            try {
                final String myPath = "/data/data/" + myContext.getPackageName()
                        + "/databases/" + myContext.getString(R.string.dbname);
                final File f = new File(myPath);
                if (f.exists())
                    return true;
                else
                    return false;
            } catch (SQLiteException e) {
                e.printStackTrace();
                return false;
            }
        }

        // --- Copy the database to the output stream---
        public void copyDataBase() throws IOException {

            final InputStream myInput = myContext.getAssets().open(
                    myContext.getString(R.string.dbname));

            final String outFileName = "/data/data/" + myContext.getPackageName()
                    + "/databases/" + myContext.getString(R.string.dbname);

            final OutputStream myOutput = new FileOutputStream(outFileName);

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

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

        public void openDataBase() throws SQLException {

            // --- Open the database---
            final String myPath = "/data/data/" + myContext.getPackageName()
                    + "/databases/" + myContext.getString(R.string.dbname);

            myDataBase = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        }

        /**
         * Close the database
         */
        public synchronized void closeDatabase() {
            if (myDataBase != null)
                ;
            myDataBase.close();
            SQLiteDatabase.releaseMemory();
            super.close();
        }


        /** This method is insert require detail in database table. **/
        public long insertMasterFence(String area, String title, String descr,
                String tag) {
            ContentValues values = new ContentValues();

            values.put(AREA, area);
            values.put(TITLE, title);
            values.put(DESCR, descr);
            values.put(TAG, tag);
            Log.v("  db ", area + " " + title + " " + " " + descr + " " + tag);
            return myDataBase.insert(MASTERFENCE_TABLE, null, values);

        }