使用外部数据库

Using the external database

我使用以下代码来使用外部数据库。但是程序停止了。谢谢你的指导。

import android.content.Context;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class DicDataBase extends SQLiteAssetHelper {


    private  static final String DATABASE_NAME = "myDataBase.db";
    public  static final int DATABASE_VERSION = 1;

    public DicDataBase(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }
}








      DicDataBase mydb = new DicDataBase(this);
       final SQLiteDatabase database = mydb.getReadableDatabase();
        Cursor data = database.rawQuery("SELECT * FROM word", null);
        while (data.moveToNext())
            try {
                Toast.makeText(getApplicationContext(), data.getString(1), Toast.LENGTH_LONG).show();
            } catch (Exception e) {

            }
    data.close();
        database.close();

为了使用您创建的数据库,您应该将数据库文件添加到 assets 并创建 class,例如 DbHelper

public class DBHelper extends SQLiteOpenHelper {

   private static String DB_PATH = "";
   private static String DB_NAME = "databasename";
   private static Integer DB_VERSION = 1;

   private static SQLiteDatabase mDataBase;

   private DBHelper(Context context) {
     super(context, DB_NAME, null, DB_VERSION);
     checkDBAddress(context);
     try {
         createDataBase(context);
         openDataBase();
     } catch (IOException e) {
        e.printStackTrace();
     }
  }

  @SuppressLint("SdCardPath")
  private void checkDBAddress(Context context) {
      if (android.os.Build.VERSION.SDK_INT >= 17) {
          DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
      } else {
          DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
      }
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
  }
  @Override
  public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {

  }
  private void createDataBase(Context context) throws IOException {
    boolean mDataBaseExist = checkDataBase();
    if (!mDataBaseExist) {
        this.getReadableDatabase();
        this.close();
        copyDataBase(context);
        logHelper.d("createDatabase database created");
    }
  }

  private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        //database does't exist yet.
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null;
 }

 private void removeDatabase() {
    try {
        String myPath = DB_PATH + DB_NAME;
        SQLiteDatabase.deleteDatabase(new File(myPath));
    } catch (Exception e) {
        logHelper.e(e);
    }
 }

//Copy the database from assets
private void copyDataBase(Context context) throws IOException {
    InputStream mInput = context.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer)) > 0) {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}

  //Open the database, so we can query it
  private boolean openDataBase() throws SQLException {
      String mPath = DB_PATH + DB_NAME;
      mDataBase = SQLiteDatabase.openDatabase(mPath, null,  SQLiteDatabase.OPEN_READWRITE);
      return mDataBase != null;
  }

  @Override
  public synchronized void close() {
      if (mDataBase != null)
          mDataBase.close();
      super.close();
  }}