2013-05-03 129 views
4

我想從資產文件夾複製SQLite數據庫。這是我的DatabaseAdapter.java類從資產文件夾複製SQLite數據庫

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) { 

    } 

} 

當我運行程序的時候,是不是錯誤。但是當我檢查數據庫文件夾我看到文件「dict.db」但只有12.00K,並且只有android_metadata表。請幫幫我。謝謝。

+1

請使用'SQLiteAssetHelper'此:https://github.com/jgilfelt/android-sqlite-asset-幫手 – CommonsWare 2013-05-03 09:50:14

回答

0

這是我的工作代碼..

void copyDataBase() throws IOException { 

     String outFileName = DB_PATH + DB_NAME; 

     OutputStream myOutput = new FileOutputStream(outFileName); 

     byte[] buffer = new byte[1024]; 
     int length; 

     InputStream myInput = myContext.getAssets().open("mydb.sqlite"); 
     while ((length = myInput.read(buffer)) > 0) { 
      myOutput.write(buffer, 0, length); 
     } 
     myInput.close(); 

     myOutput.flush(); 
     myOutput.close(); 

    } 
6

把所有這些方法在烏拉圭回合輔助類。

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 

} 

/** 
* This method will 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"); 
     } finally { 
      this.close(); 
     } 
    } 
} 

/** This method checks whether database is 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; 
    } 
} 

/** 
* This method will copy database from /assets directory to 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(); 
    } 
} 

/** This method open database for operations **/ 
public boolean openDataBase() throws SQLException { 
    String mPath = DATABASE_PATH + DATABASE_NAME; 
    myDataBase = SQLiteDatabase.openDatabase(mPath, null, 
      SQLiteDatabase.OPEN_READWRITE); 
    return myDataBase.isOpen(); 
} 

/** This method close database connection and released occupied memory **/ 
@Override 
public synchronized void close() { 
    if (myDataBase != null) 
     myDataBase.close(); 
    SQLiteDatabase.releaseMemory(); 
    super.close(); 
} 
+0

是否可以從資產中複製/創建表格? – 2016-08-11 12:55:09

+0

@PrashanthDebbadwar是的,這是可能的。 – 2016-08-11 13:00:47

+0

你可以提供任何網址嗎? – 2016-08-11 13:01:42

0
DataBaseHelper myDbHelper = new DataBaseHelper(); 
     myDbHelper = new DataBaseHelper(this); 
     try { 
      myDbHelper.createDataBase(); 

     } catch (IOException ioe) { 

      throw new Error("Unable to create database"); 
     } 

     try {  
      myDbHelper.openDataBase(); 

     }catch(SQLException sqle){ 

      throw sqle; 
     } 

用於複製數據庫這些方法是有用的

private void copyDataBase() throws IOException 
     { 
      String outFileName = DATABASE_PATH + DATABASE_NAME; 
      OutputStream myOutput = new FileOutputStream(outFileName); 
      InputStream myInput = myContext.getAssets().open(DATABASE_NAME); 

      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0) 
      { 
        myOutput.write(buffer, 0, length); 
      } 
      myInput.close(); 
      myOutput.flush(); 
      myOutput.close(); 
     } 

從參考:http://androidtutorials60.blogspot.in/2013/03/1.html

2

Android-Copy-Database爲從資產複製SQLite數據庫樣品機器人應用程序文件夾入的Android內部存儲數據庫位置。

有用

  1. 如果你不想使用代碼
  2. 創建數據庫時要使用已經填充數據庫

github下載項目並閱讀說明。

+0

感謝您的代碼庫。 – Nithinjith 2017-03-01 04:54:07

+0

「實用性」 - 幾乎任何時候您想使用SQLite並以正確的方式執行操作。 – maplemale 2017-10-25 05:42:51

0
public void CopyDatabase() throws IOException { 
    openOrCreateDatabase("you_db_file.db", 0, null); 
    InputStream mInput = getAssets().open("databases/forall.db"); 
    OutputStream mOutput = new FileOutputStream("/data/data/" + getPackageName() + "/databases/you_db_file.db"); 
    byte[] mBuffer = new byte[1024]; 
    while (true) { 
     int mLength = mInput.read(mBuffer); 
     if (mLength > 0) { 
      mOutput.write(mBuffer, 0, mLength); 
     } else { 
      mOutput.flush(); 
      mOutput.close(); 
      mInput.close(); 
      return; 
     } 
    } 
} 
0

這是做的正確的方式,使用getDatabasePath()

String dbFilePath = getDatabasePath("main.db").getAbsolutePath(); 
    File dbFile = new File(dbFilePath); 
    try { 
     InputStream inputStream = getAssets().open("main.db"); 
     OutputStream outputStream = new FileOutputStream(dbFile); 
     byte[] buffer = new byte[8192]; 
     int read; 
     while ((read = inputStream.read(buffer)) != -1) { 
      outputStream.write(buffer, 0, read); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
     throw new IllegalStateException("failed to copy db file"); 
    } 
相關問題