我有我的数据库预先创建,它会被加载到应用程序文档目录在最初的启动。我使用DB浏览器进行SQLite
在数据库中,我有一个带有y列的表x。列dataType设置为接受文本,目前是空的。列在那里,但在应用程序的早期版本中没有使用。
我想开始使用该列,但我希望它保存一个Int而不是文本。
如何更改GRDB中列的DataType?
// MARK: - Set the Default Value for the Grade Picker
func setDefaultValue(item: String, inComponent: Int)
{
if let indexPosition = K.Grading_Array.array.firstIndex(of: item)
{
picker_Outlet.selectRow(indexPosition, inComponent: inComponent, animated: false)
}
}
// MARK: - Done Btn Tapped (Grade)
@IBAction func pickerDoneBtn_Tapped(_ sender: UIButton)
{
backgroundView_Outlet.isHidden = true
// Hide the picker
UIView.animate(withDuration: 0.5, animations: { [weak self] in
self?.gradePickerView_Outlet.alpha = 0
})
if let index = K.Grading_Array.array.firstIndex(of: selectedGrade)
{
do {
try dbQueue_GRDB.write { db in
try db.execute(sql: "UPDATE My_Settings SET Default_Grade = :default_Grade WHERE Settings_ID = :id",
arguments: ["default_Grade": index, "id": 1])
}
} catch {
print("Updating the default grade failed! \(VC_String) \(error)")
}
}
}
// MARK: - Grade Fld Tapped
@IBAction func gradeFld_Tapped(_ sender: TextField_Designable)
{
sender.resignFirstResponder()
backgroundView_Outlet.isHidden = false
let gradeInt = ModelData.getThe_Default_Grade()
setDefaultValue(item: K.Grading_Array.array[gradeInt], inComponent: 0)
// Show the picker
UIView.animate(withDuration: 0.5, animations: { [weak self] in
self?.gradePickerView_Outlet.alpha = 1
})
}
// MARK: - Get the Default_Grade
static func getThe_Default_Grade() -> Int
{
var theGrade: Int = 0
do {
let settings = try dbQueue_GRDB.read { db in
try My_Settings.fetchOne(db)
}
theGrade = settings?.Default_Grade ?? 0
} catch {
print("Getting the isAnimation Status failed: (\(VC_StringX)) \(error)")
}
return theGrade
}发布于 2022-03-10 09:47:03
SQLite不支持这种表更改(更改列的类型)。为了更改列的类型,需要重新创建表。
为此:
如果您的应用程序正在使用GRDB迁移,请严格按照GRDB在https://github.com/groue/GRDB.swift/blob/master/Documentation/Migrations.md#advanced-database-schema-changes上记录的说明操作。
如果您的应用程序没有使用GRDB迁移:请严格遵循SQLite在https://www.sqlite.org/lang_altertable.html#making_other_kinds_of_table_schema_changes上记录的说明
https://stackoverflow.com/questions/71367945
复制相似问题