I have my database pre-created and it gets loaded to the apps documents directory on the initial launch. I use DB Browser for SQLite
In the database I have a table x with a column y. The column dataType is set to accept text and is currently empty. The column is there but was not used on earlier versions of the app.
I want to begin using the column but I want it to hold an Int rather than text.
How do I change the DataType of the column in GRDB?
// 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
}
SQLite does not support such table alteration (changing the type of a column) out of the box. In order to change the type of a column, you need to recreate the table.
To do so:
If your app is using GRDB migrations, follow closely the instructions documented by GRDB, at https://github.com/groue/GRDB.swift/blob/master/Documentation/Migrations.md#advanced-database-schema-changes
If your app is not using GRDB migrations: follow closely the instructions documented by SQLite, at https://www.sqlite.org/lang_altertable.html#making_other_kinds_of_table_schema_changes