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

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

问题描述

我需要在表中增加一些整数.我想通过单个查询来执行此操作,而不是进行选择和连续更新.但不走运.

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.

以下代码返回 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();
}

当然,没有更新记录.

但这会返回 1:

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

具有指定 ID 的记录 100% 存在.尝试在交易内或非交易中进行此操作 - 结果相同.没有在真正的 SQLiteStatement 上进行测试,可能是 Robolectric 错误.

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.

有人对此效果有任何假设吗?或者关于如何解决这个问题的建议(可能没有使用 SQLiteStatement)?

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

谢谢.

更新 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));

    }
}

推荐答案

根据以下来源:

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

executeUpdateDelete() 未实现.

由于callThroughByDefault = false 这个方法被默认的空实现遮蔽.这就是为什么我没有得到 RuntimeException("Stub!").

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

阴影这个方法应该很容易,但我发现了一个更多robolectric 2.0-alpha-2

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

更新 1.

拉取影子请求executeUpdateDelete():
https://github.com/pivotal/robolectric/pull/450
https://github.com/pivotal/robolectric/pull/451

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

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