开发者

Unexpected SQL query result in Android with "replace"

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜