如何解决迁移房间数据库时有没有办法使用迁移期间计算的变量?
我正在用 Java 编写一个 Android 应用程序,我已经更改了我的数据库,现在我想将其迁移到新版本,但为此我需要计算一些旧值,然后将它们插入其中一个新表进入数据库但是当我打电话
import Photos
import PhotosUI
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if #available(iOS 14,*) {
let accessLevel: PHAccessLevel = .readWrite
let authorizationStatus = PHPhotoLibrary.authorizationStatus(for: accessLevel)
switch authorizationStatus {
case .limited:
var testVc = UIViewController()
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: testVc)
//callBack(authorizationStatusAuthorised)
case .authorized:
//callBack(authorizationStatusAuthorised)
var te = ""
default:
//callBack("")
var test = ""
}
}
}
}
使用我的变量然后访问它们似乎没有将它们设置为每个值都是 0。
这是不可能的还是我只是错过了什么?
database.execSQL()
当我查看存储在 springValues 中的值时,即使我在此查询中添加了正确的值,它们也都是 0
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,type_of_clothing,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));
}
}
我可以不在这里使用 :var 符号吗?
解决方法
我建议将 SQLiteDatabase
Insert
便捷方法与 ContentValues
对象一起使用。
而不是:-
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) ");
}
你的代码可能是:-
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
-
请注意,不需要将列名括在上面使用的重音符`` 中。
-
以上代码为原理性代码,未经测试,可能存在一些错误。
如您所见,SQL 是为您构建的。 insert
方法返回插入行的 rowid(id 列)或 -1 如果插入失败,您可能希望利用此值。
您可能还希望使用其他插入方法,例如insertWithOnConflict 例如database.insertWithOnConflict("season",cv,SQLiteDatabase.CONFLICT_IGNORE);
还建议通过使用适当的 SQLiteDatabase 事务处理方法来利用事务(请参阅 beginTransaction)
它还建议您对列名使用常量,以便将名称编码到位。把列名弄错的可能性很小。