Robolectric + SQLiteStatement.executeUpdateDelete()不执行任何操作 [英] Robolectric + SQLiteStatement.executeUpdateDelete() doesn't perform any action

查看:161
本文介绍了Robolectric + SQLiteStatement.executeUpdateDelete()不执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要增加在表中的一些整数。
而不是使选择和连续更新的,我是希望通过单一查询做到这一点。
但出于运气。

继code返回0:

 最后SQLiteStatement语句= helper.getWritableDatabase()。
    compileStatement(更新my_table的一套MY_COUNT = MY_COUNT + 1,其中_id =?);
尝试
{
    stmt.bindLong(1,ID);
    返回stmt.executeUpdateDelete();
}
最后
{
    stmt.close();
}

和,当然,没有记录被更新。

但这个返回1:

 最后ContentValues​​ V =新ContentValues​​();
v.put(MY_COUNT,1);
返回helper.getWritableDatabase()。
    更新(my_table的,第五章,_id =?,新的String [] {将String.valueOf(ID)});

与指定的ID记录是100%存在。
试图在或不在交易使这 - 结果是一样的。
没有真实SQLiteStatement,大概Robolectric错误测试。

任何人有这方面的影响做任何假设?
或如何解决这个问题的建议(可能是没有的使用 SQLiteStatement )?

感谢。

更新1。

我也推出了一些简单的测试:

  @RunWith(RobolectricTestRunner.class)
公共类SQLiteStatementTest
{    私有静态类TestOpenHelper扩展SQLiteOpenHelper
    {
        公共TestOpenHelper(上下文的背景下)
        {
            超(背景下,测试,NULL,1);
        }        @覆盖
        公共无效的onCreate(SQLiteDatabase DB)
        {
            db.execSQL(CREATE TABLE测试(_id INTEGER PRIMARY KEY AUTOINCREMENT,算上INTEGER));
        }        @覆盖
        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页)
        {
        }
    }    @测试
    公共无效testExecuteUpdateDeleteModifiesRow()抛出异常
    {
        最终诠释initialCount = 6;
        最后TestOpenHelper帮手=新TestOpenHelper(Robolectric.application);        最后ContentValues​​ V =新ContentValues​​();
        v.put(计数,initialCount);
        。最后长ID = helper.getWritableDatabase()插入(测试,空,V);        最后SQLiteStatement语句= helper.getWritableDatabase()。
            compileStatement(更新测试集计数=计数+ 1,其中_id =?);        stmt.bindLong(1,ID);
        assertThat(stmt.executeUpdateDelete(),是(1));    }
}


解决方案

据下列资料来源:

<一个href=\"https://github.com/pivotal/robolectric/blob/master/src/main/java/org/robolectric/shadows/ShadowSQLiteStatement.java\" rel=\"nofollow\">https://github.com/pivotal/robolectric/blob/master/src/main/java/org/robolectric/shadows/ShadowSQLiteStatement.java

executeUpdateDelete()未实现。

由于 callThroughByDefault = FALSE 这种方法是通过的默认为空执行的阴影。这就是为什么我不明白的RuntimeException(存根!)

这应该是很容易的影子这种方法,但我发现了一个<一个href=\"http://stackoverflow.com/questions/15603583/impossible-runtimeexception-stub-with-robolectric/15772227#15772227\">more一般问题(以 Uri.parse 法)robolectric 2.0-α-2

更新1。

拉​​请求阴影 executeUpdateDelete()

https://github.com/pivotal/robolectric/pull/450

https://github.com/pivotal/robolectric/pull/451

I'm need to increment some integer in table. Instead of making select and consecutive update, i'm want to do this by single query. But out of luck.

Following code returns 0:

final SQLiteStatement stmt = helper.getWritableDatabase().
    compileStatement("update my_table set my_count = my_count + 1 where _id = ?");
try
{
    stmt.bindLong(1, id);
    return stmt.executeUpdateDelete();
}
finally
{
    stmt.close();
}

And, of course, no record is updated.

But this returns 1:

final ContentValues v = new ContentValues();
v.put("my_count", 1);
return helper.getWritableDatabase().
    update("my_table", v, "_id=?", new String[]{String.valueOf(id)});

Record with specified id is 100% exists. Tried to make this within or without transaction - result is same. Didn't test on real SQLiteStatement, probably Robolectric bug.

Anybody has any assumptions regarding this effect? Or suggestions about how to get around this problem (may be without usage of SQLiteStatement)?

Thanks.

Update 1.

I'm also roll some simple test:

@RunWith(RobolectricTestRunner.class)
public class SQLiteStatementTest
{

    private static class TestOpenHelper extends SQLiteOpenHelper
    {
        public TestOpenHelper(Context context)
        {
            super(context, "test", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL("CREATE TABLE test(_id INTEGER PRIMARY KEY AUTOINCREMENT, count INTEGER)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
        }
    }

    @Test
    public void testExecuteUpdateDeleteModifiesRow() throws Exception
    {
        final int initialCount = 6;
        final TestOpenHelper helper = new TestOpenHelper(Robolectric.application);

        final ContentValues v = new ContentValues();
        v.put("count", initialCount);
        final long id = helper.getWritableDatabase().insert("test", null, v);

        final SQLiteStatement stmt = helper.getWritableDatabase().
            compileStatement("update test set count = count + 1 where _id=?");

        stmt.bindLong(1, id);
        assertThat(stmt.executeUpdateDelete(), is(1));

    }
}

解决方案

According to following sources:

https://github.com/pivotal/robolectric/blob/master/src/main/java/org/robolectric/shadows/ShadowSQLiteStatement.java

executeUpdateDelete() isn't implemented.

Since callThroughByDefault = false this method is shadowed by default empty implementation. This is why i don't get RuntimeException("Stub!").

It should be very easy to shadow this method, but i found a more general problem (with Uri.parse method) in robolectric 2.0-alpha-2

Update 1.

Pull requests to shadow executeUpdateDelete():
https://github.com/pivotal/robolectric/pull/450
https://github.com/pivotal/robolectric/pull/451

这篇关于Robolectric + SQLiteStatement.executeUpdateDelete()不执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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