简体   繁体   English

迁移房间数据库时有没有办法使用迁移期间计算的变量?

[英]Is there a way when Migrating a Room Database to use Variables that are calculated during the Migration?

I'm writing an Android App in Java and I've changed my Database and now I want to migrate it to the new version but to do that I need to compute some of the old values and then insert those into one of the new tables into the db but when I call我正在 Java 中编写一个 Android 应用程序,我已经更改了我的数据库,现在我想将它迁移到新版本,但要做到这一点,我需要计算一些旧值,然后将它们插入到一个新表中进入数据库但是当我打电话时

database.execSQL()

with my variables and then access them it appears to not have set them as every value is 0.使用我的变量然后访问它们似乎没有设置它们,因为每个值都是 0。

Is it just not possible or did I simply miss something?这是不可能的还是我只是错过了什么?

public void migrate(@NonNull SupportSQLiteDatabase database) {
             database.execSQL("CREATE TABLE 'season' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
                    'spring' INTEGER DEFAULT 0, 'summer' INTEGER DEFAULT 0, 'autumn' INTEGER 
                    DEFAULT 0, 'winter' 
                    INTEGER DEFAULT 0, 'spook' INTEGER DEFAULT 0)");
            database.execSQL("INSERT INTO 'Clothes' (type_of_clothing,clothing_name,description,seasonId,in_laundry,TypeList) " +
                    "SELECT type_of_clothing,clothing_name,description,type_of_clothing,in_laundry,TypeList " +
                    "FROM ClothesNew");
            Cursor cursor = database.query("SELECT * FROM Clothes");
            Cursor secondCursor = database.query("SELECT * FROM ClothesSeason");
            ArrayList<Season> seasonsToBeAdded = new ArrayList<>();
            while (cursor.moveToNext()) {
                secondCursor.moveToNext();
                int Sid = cursor.getInt(0);
                String seasonString = secondCursor.getString(2);
                Season season = new Season(seasonString);
                int tempId = Sid;
                boolean isInList = false;
                for (Season tempSeason : seasonsToBeAdded) {
                    if (tempSeason.equals(season)) {
                        tempId = seasonsToBeAdded.indexOf(tempSeason);
                        isInList = true;
                        break;
                    }
                }
                if (!isInList) {
                    season.setId(seasonsToBeAdded.size());
                    tempId = season.getId();
                    seasonsToBeAdded.add(season);
                }
                if (seasonsToBeAdded.size() == 0) {seasonsToBeAdded.add(season);}
                
                database.execSQL("UPDATE Clothes SET seasonId = :tempId WHERE uid = :Sid");
            }

            int id, spring, summer, autumn, winter, spook;

            for (Season season : seasonsToBeAdded) {
                id = season.getId();
                spring = season.getSpring();
                summer = season.getSummer();
                autumn = season.getAutumn();
                winter = season.getWinter();
                spook = season.getSpook();
                database.execSQL("INSERT INTO season " +
                        "VALUES(:id, :spring, :summer, :autumn, :winter, :spook) ");
            }
            Cursor otherCursor = database.query("SELECT * FROM season");
            ArrayList<Integer> springValues= new ArrayList<>();
            while (otherCursor.moveToNext()) {
                springValues.add(otherCursor.getInt(1));
            }
        }

When I look at the Values stored in springValues they are all 0 even though I add the correct ones in this query当我查看存储在 springValues 中的值时,即使我在此查询中添加了正确的值,它们也都是 0

database.execSQL("INSERT INTO season " +
                 "VALUES(:id, :spring, :summer, :autumn, :winter, :spook) ");

Can I just not use the:var notation here?我可以不在这里使用:var 符号吗?

I'd suggest utilising The SQLiteDatabase Insert convenience method along with a ContentValues object.我建议使用SQLiteDatabase Insert便捷方法和ContentValues object。

Instead of:-代替:-

        for (Season season : seasonsToBeAdded) {
            id = season.getId();
            spring = season.getSpring();
            summer = season.getSummer();
            autumn = season.getAutumn();
            winter = season.getWinter();
            spook = season.getSpook();
            database.execSQL("INSERT INTO season " +
                    "VALUES(:id, :spring, :summer, :autumn, :winter, :spook) ");
        }

Your code could then be:-您的代码可能是:-

    ContentValues cv = new ContentValues(); // Instantiate ContentValues object
    for (Season season : seasonsToBeAdded) {
        cv.clear();
        cv.put("`id`",season.getId());
        cv.put("`spring`",season.getSpring());
        cv.put("`summer`",season.getSummer());
        cv.put("`autumn`",season.getAutumn());
        cv.put("`winter`",season.getWinter());
        cv.put("`spook`",season.getSpook());
        database.insert("season",null,cv); //<<<< Recommended Uses the convenience Insert Method
  • Note there should be no need to enclose the column names in grave accents `` as used above.请注意,不需要将列名括在上面使用的重音 `` 中。

  • The above code is in-principle code and has not been tested, as such it may contain some errors.以上代码为原理代码,未经测试,可能存在一些错误。

As you can see the SQL is built for you.如您所见,SQL 专为您打造。 The insert method returns the rowid (id column) of the inserted row or -1 if the insert failed, you may wish to take advantage of this value. insert方法返回插入行的 rowid(id 列),如果插入失败,则返回 -1,您可能希望利用此值。

You may also wish to utilise other insert methods eg insertWithOnConflict eg database.insertWithOnConflict("season",null,cv,SQLiteDatabase.CONFLICT_IGNORE);您可能还希望使用其他插入方法,例如insertWithOnConflict例如database.insertWithOnConflict("season",null,cv,SQLiteDatabase.CONFLICT_IGNORE);

It would also be advisable to utilise transactions by utilising the appropriate SQLiteDatabase transaction handling methods (see beginTransaction )还建议通过使用适当的 SQLiteDatabase 事务处理方法来利用事务(请参阅beginTransaction

It also also recommended that you utilise constants for column names so the name is coded in just on place.它还建议您对列名使用常量,以便将名称编码到位。 So little chance of getting the column names wrong.列名错误的可能性很小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM