Android 中使用“替换”进行意外 SQL 查询结果

发布于 2024-11-02 19:41:18 字数 783 浏览 1 评论 0原文

我正在为 Android 编写一个应用程序,它会定期下载游戏排行榜。结果存储在数据库中。为了使下载与实时结果分开,我下载到一个表中,然后复制到另一个表中。排行榜表存储当前排名和先前排名。在将下载的版本复制到实时版本之前,我会从当前的 ie 中更新以前的评级:

mDb.execSQL(
      "update " + mTableName + " set last_rank = rank, last_world = world;");         

然后,我从下载表中选择适当的列并替换到​​实时表中:

mDb.execSQL(
     "replace into " + mTableName + " (world, _id, victories, owned_regions, rank,
     clan_name) select world, _id, victories, owned_regions, rank, clan_name
     from " + mTableName + "_dl;");

如果我在更新之后但在更新之前获得数据库的副本替换然后运行 ​​Android 外部的替换查询(我使用 SQLite Expert 进行测试),然后我得到预期的结果 - last_rank 和 last_world 字段被保留,但如果我在 Android 中运行查询,则 _dl 的所有字段表被复制,并且last_rank 和last_world 被用tehir 默认值覆盖。这是 Android SQLite 实现中的错误还是我做错了什么?

任何帮助将不胜感激。谢谢。

I am writing an application for Android that downloads a games leaderboard at frequent intervals. The results are stored in a database. To keep a download seperate from the live results I download into one table then copy over to another. The leaderboard table stores a current ranking and a previous ranking. Before copying the downloaded version into the live version I update the previous rating from the current i.e.:

mDb.execSQL(
      "update " + mTableName + " set last_rank = rank, last_world = world;");         

Then I select the appropriate columns from the download table and replace into the live table:

mDb.execSQL(
     "replace into " + mTableName + " (world, _id, victories, owned_regions, rank,
     clan_name) select world, _id, victories, owned_regions, rank, clan_name
     from " + mTableName + "_dl;");

If I get a copy of the database after the update but before the replace then run the replace query external to Android (I use SQLite Expert for my tests), then I get the expected results - the last_rank and last_world fields are preserved, but if I run the query in Android, then all the fields of the _dl table are copied over and last_rank and last_world are over-written with tehir default values. Is this a bug in the Android SQLite implementation or am I doing something very wrong?

Any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

累赘 2024-11-09 19:41:18

再次回答我自己的问题,尽管这与其说是答案,不如说是解决方法。解决方案是对现有记录集进行外部联接。

String sql = String.format(
    "replace into %s " +
    "select %s.world, " +
    "%s._id, " +
    "%s.victories, " +
    "%s.owned_regions, " +
    "%s.rank, " +
    "%s.rank as last_rank, " +
    "%s.world as last_world, " +
    "%s.clan_name, " +
    "%s.updated from " +
    "%s left outer join " +
    "%s on " +
    "%s._id = " +
    "%s._id", 
    mTableName, // replace into 
    mTableName + "_dl", // world
    mTableName + "_dl", // ._id
    mTableName + "_dl", // victories                    
    mTableName + "_dl", // owned regions                    
    mTableName + "_dl", // dl.current_Rank
    mTableName, // prev rank
    mTableName, // prev world
    mTableName + "_dl", // clan name
    mTableName + "_dl", // updated
    mTableName + "_dl", // left outer join
    mTableName, // on
    mTableName, // tablename._id
    mTableName + "_dl"); // tablename_dl.id

mSynchronizeQuery = db.compileStatement(sql);


mSynchronizeQuery.execute();

我希望其他人发现这很有用。

Answering my own question again, although this isn't so much an answer as a workaround. The solution was to do an outer join with the existing record set.

String sql = String.format(
    "replace into %s " +
    "select %s.world, " +
    "%s._id, " +
    "%s.victories, " +
    "%s.owned_regions, " +
    "%s.rank, " +
    "%s.rank as last_rank, " +
    "%s.world as last_world, " +
    "%s.clan_name, " +
    "%s.updated from " +
    "%s left outer join " +
    "%s on " +
    "%s._id = " +
    "%s._id", 
    mTableName, // replace into 
    mTableName + "_dl", // world
    mTableName + "_dl", // ._id
    mTableName + "_dl", // victories                    
    mTableName + "_dl", // owned regions                    
    mTableName + "_dl", // dl.current_Rank
    mTableName, // prev rank
    mTableName, // prev world
    mTableName + "_dl", // clan name
    mTableName + "_dl", // updated
    mTableName + "_dl", // left outer join
    mTableName, // on
    mTableName, // tablename._id
    mTableName + "_dl"); // tablename_dl.id

mSynchronizeQuery = db.compileStatement(sql);


mSynchronizeQuery.execute();

I hope someone else finds this useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文