使用Robolectric与SQLiteAssetHelper [英] Using Robolectric with SQLiteAssetHelper

查看:342
本文介绍了使用Robolectric与SQLiteAssetHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的Robolectric,所以提前道歉,如果我想的东西很明显这里。我有一个加载从资产使用的 SQLiteAssetHelper ,我想测试使用 Robolectric 框架数据库。我能够获取数据库的引用,但它似乎是空的。 SQLiteAssetHelper输出成功打开数据库消息日志,但在数据库中没有表。

I'm very new to Robolectric, so apologies in advance if I'm missing something obvious here. I have an app that loads a database from the assets directory using an SQLiteAssetHelper, and I'd like to test that database using the Robolectric framework. I'm able to get a reference to the database, but it seems to be empty. SQLiteAssetHelper outputs a "successfully opened database" message to the log, but there are no tables in the database.

编辑:这里的code我用得到使用下面张贴的类@ user2953017方法在我的设置数据库

Here's the code I'm using to get the database in my setUp method using the class @user2953017 posted below:

@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异常的原因是,在查询中指定的表名是找不到的。

The cause of the SQL exception is that the table name specified in the query isn't found.

我会的任何意见表示感谢。

I'd be grateful for any advice.

推荐答案

首先从资产文件夹中的数据库复制到您的应用程序数据库文件夹。
这里是code将复制数据库

First copy your database from asset folder to your app database folder. Here is the code that will copy the database

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.");
  }
  }

还有清洁香港这个链接..它可能是有用的ü
<一href=\"http://stackoverflow.com/questions/7320820/testing-sqlite-database-in-robolectric?rq=1\">Testing在Robolectric SQLite数据库

试试这些东西:

1.Delete数据库从终端

1.Delete your database from terminal

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

和仿真器上再重新安装应用程序。

and re-install your application again on emulator.


  1. 试着增加模拟器的RAM ..同样的错误来了我,尽管我都在数据库中,但是当我增加了模拟器的RAM 1024年至2000年。它的工作数据。

  1. Try to increase the RAM of emulator.. Same error was coming to me despite of my all the data in the database but when I increased the RAM of emulator from 1024 to 2000 .. It worked.

从DDMS数据库复制到系统文件系统和打开直通源码的浏览器,并检查表是否存在与否。
链接sqlite的浏览器
http://sourceforge.net/projects/sqlitebrowser/files/sqlitebrowser/

Copy your database from DDMS to your system file system and open it thru sqlite browser and check whether Table exist or not. Link for sqlite browser http://sourceforge.net/projects/sqlitebrowser/files/sqlitebrowser/

这篇关于使用Robolectric与SQLiteAssetHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆