2011-06-30 20 views
41

पर एक SQLite डेटाबेस का सरल निर्यात और आयात मैं बैकअप उद्देश्यों के लिए एक सरल SQLite निर्यात/आयात को लागू करने की कोशिश कर रहा हूं। निर्यात केवल कच्चे current.db फ़ाइल की एक प्रति संग्रहित करने का मामला है। मैं आयात के लिए क्या करना चाहता हूं केवल पुराने current.db फ़ाइल को हटाना है और imported.db फ़ाइल का नाम current.db पर पुनर्नामित करना है। क्या यह संभव है?एंड्रॉइड

06-30 13:33:38.831: ERROR/SQLiteOpenHelper(23570): 
    android.database.sqlite.SQLiteDatabaseCorruptException: error code 11: database disk image is malformed 

अगर मैं एक SQLite ब्राउज़र में कच्चे डेटाबेस फ़ाइल को देखो यह ठीक लग रहा है: जब मैं इस समाधान का प्रयास करें, मैं निम्नलिखित त्रुटि मिलती है।

उत्तर

101

मैं डेटाबेस कोड आयात करने के लिए अपने अनुप्रयोगों में से एक में SQLiteOpenHelper में इस कोड का उपयोग करता हूं।

संपादित करें: मैंने प्रश्न में मेरी FileUtils.copyFile() विधि चिपका दी।

SQLiteOpenHelper

public static String DB_FILEPATH = "/data/data/{package_name}/databases/database.db"; 

/** 
* Copies the database file at the specified location over the current 
* internal application database. 
* */ 
public boolean importDatabase(String dbPath) throws IOException { 

    // Close the SQLiteOpenHelper so it will commit the created empty 
    // database to internal storage. 
    close(); 
    File newDb = new File(dbPath); 
    File oldDb = new File(DB_FILEPATH); 
    if (newDb.exists()) { 
     FileUtils.copyFile(new FileInputStream(newDb), new FileOutputStream(oldDb)); 
     // Access the copied database so SQLiteHelper will cache it and mark 
     // it as created. 
     getWritableDatabase().close(); 
     return true; 
    } 
    return false; 
} 

FileUtils

public class FileUtils { 
    /** 
    * Creates the specified <code>toFile</code> as a byte for byte copy of the 
    * <code>fromFile</code>. If <code>toFile</code> already exists, then it 
    * will be replaced with a copy of <code>fromFile</code>. The name and path 
    * of <code>toFile</code> will be that of <code>toFile</code>.<br/> 
    * <br/> 
    * <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by 
    * this function.</i> 
    * 
    * @param fromFile 
    *   - FileInputStream for the file to copy from. 
    * @param toFile 
    *   - FileInputStream for the file to copy to. 
    */ 
    public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException { 
     FileChannel fromChannel = null; 
     FileChannel toChannel = null; 
     try { 
      fromChannel = fromFile.getChannel(); 
      toChannel = toFile.getChannel(); 
      fromChannel.transferTo(0, fromChannel.size(), toChannel); 
     } finally { 
      try { 
       if (fromChannel != null) { 
        fromChannel.close(); 
       } 
      } finally { 
       if (toChannel != null) { 
        toChannel.close(); 
       } 
      } 
     } 
    } 
} 

यदि आवश्यक हो तो पुराने डेटाबेस फ़ाइल को नष्ट करने के लिए मत भूलना।

+0

इस पूरी तरह से काम किया है बहुत बहुत धन्यवाद! –

+0

यह एक बहुत अच्छा समाधान है। खुश होती है। –

+4

अपने पसंदीदा उत्तरों को अप-वोट करना याद रखें, समुदाय को यह जानने में सहायता करता है कि कौन से अच्छे हैं और जो जंक हैं। –

29

यह एक ही फ़ोल्डर एक

public class ExportImportDB extends Activity { 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
//creating a new folder for the database to be backuped to 
      File direct = new File(Environment.getExternalStorageDirectory() + "/Exam Creator"); 

       if(!direct.exists()) 
       { 
        if(direct.mkdir()) 
         { 
         //directory is created; 
         } 

       } 
      exportDB(); 
      importDB(); 

     } 
    //importing database 
     private void importDB() { 
      // TODO Auto-generated method stub 

      try { 
       File sd = Environment.getExternalStorageDirectory(); 
       File data = Environment.getDataDirectory(); 

       if (sd.canWrite()) { 
        String currentDBPath= "//data//" + "PackageName" 
          + "//databases//" + "DatabaseName"; 
        String backupDBPath = "/BackupFolder/DatabaseName"; 
        File backupDB= new File(data, currentDBPath); 
        File currentDB = new File(sd, backupDBPath); 

        FileChannel src = new FileInputStream(currentDB).getChannel(); 
        FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
        dst.transferFrom(src, 0, src.size()); 
        src.close(); 
        dst.close(); 
        Toast.makeText(getBaseContext(), backupDB.toString(), 
          Toast.LENGTH_LONG).show(); 

       } 
      } catch (Exception e) { 

       Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG) 
         .show(); 

      } 
     } 
    //exporting database 
     private void exportDB() { 
      // TODO Auto-generated method stub 

      try { 
       File sd = Environment.getExternalStorageDirectory(); 
       File data = Environment.getDataDirectory(); 

       if (sd.canWrite()) { 
        String currentDBPath= "//data//" + "PackageName" 
          + "//databases//" + "DatabaseName"; 
        String backupDBPath = "/BackupFolder/DatabaseName"; 
        File currentDB = new File(data, currentDBPath); 
        File backupDB = new File(sd, backupDBPath); 

        FileChannel src = new FileInputStream(currentDB).getChannel(); 
        FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
        dst.transferFrom(src, 0, src.size()); 
        src.close(); 
        dst.close(); 
        Toast.makeText(getBaseContext(), backupDB.toString(), 
          Toast.LENGTH_LONG).show(); 

       } 
      } catch (Exception e) { 

       Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG) 
         .show(); 

      } 
     } 

    } 

न जोड़ना भूल से डेटाबेस आयात करने के लिए और बैकअप फ़ोल्डर आप इसे नाम कर सकते हैं के रूप में आप चाहते हैं नाम का एक फ़ोल्डर एक सरल विधि के लिए डेटाबेस निर्यात करने के लिए एक सरल तरीका है इस अनुमति के आगे बढ़ने के लिए यह

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" > 
    </uses-permission> 

का आनंद लें

+1

किसी भी तरह importDB और exportDB प्रक्रियाएं बिल्कुल समान हैं? – Taifun

+1

गलत: यदि आप बारीकी से देखते हैं, तो 'currentDB' वैरिएबल importDB में डेटा फ़ोल्डर फ़ाइल में और निर्यात डीबी में एसडी फ़ोल्डर को संदर्भित करता है। – ravemir

+2

क्या यह गैर-रूट डिवाइस पर काम करेगा? – Sarfraz