android - Copy SQLite database from assets folder -


i wanna copy sqlite database assets folder. 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 run app isn't error. when check databases folder saw file "dict.db" 12.00k , has android_metadata table. please me. thanks.

put 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  }  /**  * method 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");         } {             this.close();         }     } }  /** method checks whether database 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;     } }  /**  * method copy database /assets directory 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();     } }  /** method open database operations **/ public boolean opendatabase() throws sqlexception {     string mpath = database_path + database_name;     mydatabase = sqlitedatabase.opendatabase(mpath, null,             sqlitedatabase.open_readwrite);     return mydatabase.isopen(); }  /** method close database connection , released occupied memory **/ @override public synchronized void close() {     if (mydatabase != null)         mydatabase.close();     sqlitedatabase.releasememory();     super.close(); } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -