簡體   English   中英

將數據庫與APK文件一起添加到Android

[英]Adding database along with apk file android

我按照教程將sqlite數據庫添加到apk中,但是應用程序強制在我的手機上關閉

我將sqliteopenhelper修改如下:

public class openingclass extends SQLiteOpenHelper  
    {  
        public openingclass(Context c) {  
            super(c,Db_NAME, null, DB_VERSION);  
        }  
         public void createDataBase() {  

                boolean dbExist;  
                try {  

                     dbExist = checkDataBase();  


                } catch (SQLiteException e) {  

                    e.printStackTrace();  
                    throw new Error("database dose not exist");  

                }  

                if(dbExist){  
                //do nothing - database already exist  
                }else{  

                    try {  

                        copyDataBase();  


                    } catch (IOException e) {  

                        e.printStackTrace();  
                        throw new Error("Error copying database");  

                    }  
            //By calling this method and empty database will be created into the default system path  
            //of your application so we are gonna be able to overwrite that database with our database.  
                this.getReadableDatabase();  


            }  

            }  

            /** 
              * 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_READONLY);  
            }catch(SQLiteException e){  

            //database does't exist yet.  
                throw new Error("database does't exist yet.");  

            }  

            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. 
              * */  
            private void copyDataBase() throws IOException{  



                    //copyDataBase();  
                    //Open your local db as the input stream  
                    InputStream myInput = c1.getAssets().open(Db_NAME);  

                    // Path to the just created empty db  
                    String outFileName = DB_PATH +"/"+ Db_NAME;  
                    File databaseFile = new File(DB_PATH);  
                     // check if databases folder exists, if not create one and its subfolders  
                    if (!databaseFile.exists()){  
                        databaseFile.mkdir();  
                    }  

                    //Open the empty db as the output stream  
                    OutputStream myOutput = new FileOutputStream(outFileName);  

                    //transfer bytes from the inputfile to the outputfile  
                    byte[] buffer = new byte[1024];  
                    int length;  
                    while ((length = myInput.read(buffer))>0){  
                    myOutput.write(buffer, 0, length);  
                    }  

                    //Close the streams  
                    myOutput.flush();  
                    myOutput.close();  
                    myInput.close();  



            }  



            @Override  
            public synchronized void close() {  

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

                super.close();  

            }  

        @Override  
        public void onCreate(SQLiteDatabase arg0) {  
            String S = "create table " +  
                    TABLE_NAME +  
                    " (" +  
                    TABLE_COL_MAIL + " text primary key," +  
                    TABLE_COL_NAME + " text," +  
                    TABLE_COL_PASS + " text," +  
                    TABLE_COL_PHO + " text," +  
                    TABLE_COL_ADD + " text," +  
                    TABLE_COL_GEN + " text," +  
                    TABLE_COL_DOB + " text" +  
                    ");";  
            arg0.execSQL(S);  

            String S1 = "create table " +  
                    SECOND_TABLE_NAME +  
                    " (" +  
                    TABLE_COL_USER + " text," +  
                    TABLE_COL_PRODUCT + " text," +  
                    TABLE_COL_QUANTITY + " integer" +  
                    ");";  
            arg0.execSQL(S1);  




        }  

        @Override  
        public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {  
            // TODO Auto-generated method stub  

        } 

我也很難編碼路徑,我不確定。

  private final String DB_PATH="/data/data/com.example.shopkart/databases/";  
        private final String Db_NAME = "dbshopkart.db"; 

其他線程告訴我該路徑不正確。如何設置正確的路徑(有/沒有硬編碼)。請幫忙!

File publicDir = Environment.getExternalStorageDirectory();

publicDir.getAbsoluteFile();

也許這會幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM