android - 访问外部 SQLite 数据库时出错

标签 android sqlite android-sqlite ormlite

我有一个名为 example.db 的外部 sqlite 数据库,我正在尝试使用 OrmLite 创建它。我在网上搜索了一下,大家都推荐了这个教程http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

所以我遵循它并且工作得很好,但是当我尝试使用 OrmLite 做同样的事情时,我无法从外部文件(example.db)创建我自己的数据库。我发现当我调用方法 createDatabase() 时,数据库已经创建,并且当我在没有 OrmLite 的情况下使用 OpenHelperDatabase 时它没有发生。

有人知道吗?

代码是:

//imports

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

private static String DB_PATH = "/data/data/com.example/databases/";
private static String DB_NAME = "example.db";
private static final int DATABASE_VERSION = 1;

private SQLiteDatabase exampleDB;
private final Context context;

public DatabaseHelper(Context context) {

    super(context, DB_NAME, null, DATABASE_VERSION);
    this.context = context;
}

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if(dbExist){
        Log.i(DatabaseHelper.class.getName(), "Database already exist!!! Here is the problem");
    }else{

        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

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 ? true : false;
}

private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = context.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(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    Log.i(DatabaseHelper.class.getName(), "onCreate");
    try {
        createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
   //TODO
}


@Override
public synchronized void close() {

    if(exampleDB != null)
        exampleDB.close();

    super.close();

}
}

最佳答案

createDataBase 方法

public void createDataBase() throws IOException {  

    boolean dbExist = checkDataBase();  

    if (dbExist) {  

    } else {  
        this.getReadableDatabase();  

        try {  
        copyDataBase(); 

        } catch (IOException e) {  
            throw new Error("Error copying database");  
        }  
    }  
}  

private boolean checkDataBase() {  
    File dbFile = new File(DB_PATH + DB_NAME);  
    return dbFile.exists();  
}  

复制数据库方法

private void copyDataBase() throws IOException {  

      InputStream myInput = context.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);  
      }  
      // Close the streams  
      myOutput.flush();  
      myOutput.close();  
      myInput.close();  

     }  

开放数据库方法

public void openDataBase () throws SQLException{
    String path = DB_PATH +DB_NAME;
    db =SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
}

从数据库中检索数据

public Cursor getData() {  

      String myPath = DB_PATH + DB_NAME;  
      db = SQLiteDatabase.openDatabase(myPath, null,  
        SQLiteDatabase.OPEN_READONLY);  
      Cursor c = db.rawQuery("SELECT * FROM info", null);  
          return c;  
     }  

将数据插入数据库

public void addUser(Astro contact) {  
  //  SQLiteDatabase db = this.getWritableDatabase();  
    String myPath = DB_PATH + DB_NAME;  
    db = this.getWritableDatabase();

    ContentValues values = new ContentValues();  
    values.put(KEY_Date, contact.get_date()); // Contact Name  
    values.put(KEY_Time, contact.get_time()); // Contact Phone  
    // Inserting Row  
    db.insert(TABLE_ASTRO, null, values);  
    //2nd argument is String containing nullColumnHack  
    db.close(); // Closing database connection  
}

关于android - 访问外部 SQLite 数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27780544/

相关文章:

java - Android - 尝试在 onCreate() 内调用另一个类的方法

sql - 查找所有用户共有的项目

Android Room 缓慢迁移竞争条件

java - 如何随机将文本设置为 SQLite 数据库中的四个按钮?

android - 可以在 iOS 设备上强制预加载 HTML5 视频吗?

android - 如何在android中的字符串中的每个 "\n"之后添加行间距?

c++ - 使用 Qt 从 SQLite 中选择

java - 保存一些数据 sqlite、SharedPreferences 或文件?

android - 如何通过从 Assets 中销毁和重新加载数据库来迁移 Room 数据库

java - Android:如何更改 DatePicker 值