簡體   English   中英

將Robolectric與SQLiteAssetHelper一起使用

[英]Using Robolectric with SQLiteAssetHelper

我是Robolectric的新手,如果我在這里遺漏了一些明顯的東西,請提前道歉。 我有一個應用程序使用SQLiteAssetHelperassets目錄加載數據庫,我想使用Robolectric框架測試該數據庫。 我能夠獲得對數據庫的引用,但它似乎是空的。 SQLiteAssetHelper向日志輸出“已成功打開的數據庫”消息,但數據庫中沒有表。

編輯:這是我使用下面發布的類@ user2953017在我的setUp方法中獲取數據庫的代碼:

@Before
public void setUp() {
    Context context = Robolectric.getShadowApplication().getApplicationContext();
    SQLiteDatabase db = (new DataBaseWrapper(context)).getReadableDatabase();

    Cursor c = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
    if (!c.moveToFirst()) {
        Log.w("debug", "No tables!");
    }  

    // My second query here fails, since the database contains no tables.
}

來自logcat:

W/debug: No tables!
DEBUG: Loading resources for android from jar:/home/<name>/.m2/repository/org/robolectric/android-res/4.1.2_r1_rc/android-res-4.1.2_r1_rc-real.jar!/res...
E/Debug: java.lang.RuntimeException: SQL exception in query

SQL異常的原因是找不到查詢中指定的表名。

我很感激任何建議。

首先將數據庫從asset文件夾復制到app數據庫文件夾。 這是將復制數據庫的代碼

public class DataBaseWrapper extends SQLiteOpenHelper
 {
  private static String TAG = DataBaseWrapper.class.getName();
  private  String DB_PATH; //= "/data/data/com.example.yourproject/databases/";
  private static String DB_NAME = "Database.sqlite";
  private SQLiteDatabase myDataBase = null; 
  private final Context myContext;

  public DataBaseWrapper(Context context) 
  {
     super(context, DB_NAME, null, 1);

      this.myContext = context;
      DB_PATH="/data/data/" + context.getPackageName() + "/" + "databases/";
      Log.v("log_tag", "DBPath: " + DB_PATH);
     //  File f=getDatabasePath(DB_NAME);
  } 

  public void createDataBase() throws IOException{
   boolean dbExist = checkDataBase();
   if(dbExist){
    Log.v("log_tag", "database does exist");
    }else{
     Log.v("log_tag", "database does not exist");
     this.getReadableDatabase();
     try {
      copyDataBase();
        } catch (IOException e) {
      throw new Error("Error copying database");
      }
    }
   }

  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();
  }

  private boolean checkDataBase(){

     File dbFile = new File(DB_PATH + DB_NAME); 
     //Log.v("dbFile", dbFile + "   "+ dbFile.exists()); 
     return dbFile.exists(); 

 }

 public boolean openDataBase() throws SQLException
 {
    String mPath = DB_PATH + DB_NAME; 
    //Log.v("mPath", mPath); 
    myDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY); 
    //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    return myDataBase != null; 

 }


  @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) 
 {
    Log.v(TAG, "Upgrading database, this will drop database and recreate.");
  }
  }

另外chk這個鏈接..它可能對你在Robolectric中測試SQLite數據庫很有用

試試這些:

1.從終端刪除您的數據庫

adb shell
cd /data/data/com.example.apploicationname/databases
rm *

並在模擬器上重新安裝您的應用程序。

  1. 嘗試增加模擬器的RAM ..雖然我的數據庫中的所有數據但是當我將模擬器的RAM從1024增加到2000時,同樣的錯誤即將到來。它工作。

  2. 將數據庫從DDMS復制到系統文件系統,然后通過sqlite瀏覽器打開它,檢查Table是否存在。 鏈接sqlite瀏覽器http://sourceforge.net/projects/sqlitebrowser/files/sqlitebrowser/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM