SQLite
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> SQLite

sao chép tệp cơ sở dữ liệu từ / asset sang thư mục / data / data trong trình khám phá tệp - Android

Hãy thử sử dụng mã bên dưới để sao chép cơ sở dữ liệu từ nội dung sang thư mục data / data / package

package com.example.myapp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

      public class DataBaseHelper1 extends SQLiteOpenHelper{
      private static String DB_PATH = "/data/data/com.example.myapp/databases/";

      private static String DB_NAME = "myDB.sqlite";

      private SQLiteDatabase myDataBase;

      private final Context myContext;

      public DataBaseHelper1(Context context) 
      {
          super(context, DB_NAME, null, 1);
          this.myContext = context;
      }

      public void createDataBase() throws IOException{


    boolean dbExist = checkDataBase();

      if(dbExist)
      {
          Log.i("DB....", "database available....");
      }
      else
      {
          this.getWritableDatabase();

          try {

          copyDataBase();

          } catch (IOException e) {

          throw new Error("Error copying database");

          }

         Log.i("DB..", "database created.....");
       }   

      }


      public boolean checkDataBase(){

      SQLiteDatabase checkDB = null;

      try{

      String myPath = DB_PATH + DB_NAME;

      checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

      }catch(SQLiteException e){

          Log.e("CheckDb","DB not found");
      //database does't exist yet.

      if(checkDB != null){

      checkDB.close();

      }
      }
      finally
      {
          if(checkDB != null){

              checkDB.close();

              } 
          this.close();
      }
      return checkDB != null ? true : false;

      }




      private void copyDataBase() throws IOException{

      InputStream myInput = myContext.getAssets().open(DB_NAME);

      String outFileName = DB_PATH + DB_NAME;

      OutputStream myOutput = new FileOutputStream(outFileName);
          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 SQLiteDatabase openDataBase() throws SQLException{

      String myPath = DB_PATH + DB_NAME;

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

      }


      @Override

      public synchronized void close() {

      if(myDataBase != null)

      myDataBase.close();

      super.close();

      }

      @Override

      public void onCreate(SQLiteDatabase db) {

      }

      @Override

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


      }  


      public void getData()
      {
          SQLiteDatabase myDB ;
          Cursor cursor ;
           try {

                myDB=this.openDataBase();                   

                    cursor=myDB.rawQuery("SELECT * FROM Country_Master",null);


                    if (cursor != null ) {
                       if  (cursor.moveToFirst()) {
                       do {

                           // put your code to get data from cursor                      

                       }while (cursor.moveToNext());
                       }

                    }



                   if(cursor != null)
                    {
                        myDB.close();
                       cursor.close();
                    }                      
                    }catch(SQLException sqle){

                    throw sqle;

                    }


        }
}

Đồng thời đặt mã này dưới phương thức onCreate trong hoạt động của bạn nơi bạn muốn sao chép cơ sở dữ liệu ..

 DataBaseHelper1 myDbHelper = new DataBaseHelper1(MyActivity.this); 
     try 
     {
         myDbHelper.createDataBase();
     }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }

     finally
     {
         myDbHelper.close();
     }



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Làm cách nào tôi có thể đọc dữ liệu từ một DB được mã hóa bằng SQLiteAssetHelper?

  2. Phát triển cơ sở dữ liệu Android SQLite MultiTable

  3. SQLite giới thiệu hàm UNIXEPOCH ()

  4. SQLite - Thả một cơ sở dữ liệu

  5. Trừ số năm cho một ngày trong SQLite