انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
کپی کردن پایگاه داده از پوشه‌ی assets به data (حافظه‌ی داخلی) (اندروید)

کپی کردن پایگاه داده از پوشه‌ی assets به data (حافظه‌ی داخلی) (اندروید)

#7
با کمک دوستان عزیزی که من را راهنمایی کردند و این سایت موقق به کپی کردن پایگاه داده به آدرس  /data/data/PACKAGE_NAME/databases/   در حافظه داخلی شدم، این آموزش را برای کسانی که مشکل دارند می‌گذارم:
1. ابتدا یک کلاس با نام DataBaseHelper از گسترش SQLiteOpenHelper ساختم و کد زیر را داخل آن قرار دادم:

کد پی‌اچ‌پی:
public class DataBaseHelper extends SQLiteOpenHelper{
 
    
//The Android's default system path of your application database.
    
private static String DB_PATH "/data/data/YOUR_PACKAGE/databases/";  //در این قسمت نام پکیج جایگزین شود
 
    
private static String DB_NAME "myDBName"//در این قسمت تنها نام فایل دیتابیس که درون پوشه قرار دارد جایگزین شود
 
    
private SQLiteDatabase myDataBase
 
    private final 
Context myContext;
 
  
    public 
DataBaseHelper(Context context) {
 
        
super(contextDB_NAMEnull1);
        
this.myContext context;
    }    
 
  
/**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    
public void createDataBase() throws IOException{
 
        
boolean dbExist checkDataBase();
 
        if(
dbExist){
            
//do nothing - database already exist
        
}else{
 
            
//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();
 
            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(myPathnullSQLiteDatabase.OPEN_READONLY);
 
        }catch(
SQLiteException e){
 
            
//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{
 
        
//Open your local db as the input stream
        
InputStream myInput myContext.getAssets().open(DB_NAME);
 
        
// Path to the just created empty db
        
String outFileName DB_PATH DB_NAME;
 
        
//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(buffer0length);
        }
 
        
//Close the streams
        
myOutput.flush();
        
myOutput.close();
        
myInput.close();
 
    }
 
    public 
void openDataBase() throws SQLException{
 
        
//Open the database
        
String myPath DB_PATH DB_NAME;
        
myDataBase SQLiteDatabase.openDatabase(myPathnullSQLiteDatabase.OPEN_READONLY);
 
    }
 
    @
Override
    
public synchronized void close() {
 
            if(
myDataBase != null)
                
myDataBase.close();
 
            
super.close();
 
    }
 
    @
Override
    
public void onCreate(SQLiteDatabase db) {
 
    }
 
    @
Override
    
public void onUpgrade(SQLiteDatabase dbint oldVersionint newVersion) {
 
    }
 
        
// Add your public helper methods to access and get content from the database.
       // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
       // to you to create adapters for your views.
 


2.سپس در کلاس دیگری که قصد داشتم دیتابیس ساخته شود کد زیر را قرار دادم:

کد پی‌اچ‌پی:
DataBaseHelper db = new DataBaseHelper(context);
   try 
    {
        
db.createDataBase();
    } 
    catch (
IOException io
    {
        throw new 
Error("Unable to create database");
    } 
پاسخ
 سپاس شده توسط داش بهروز ، admin ، شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پیام‌های این موضوع
RE: کپی کردن پایگاه داده از پوشه‌ی assets به data (حافظه‌ی داخلی) (اندروید) - توسط ramtin2080 - ۱۳۹۴/۰۱/۱۱, ۰۵:۳۷ ب.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان