首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用GRDB将列的dataType从文本更改为int

如何用GRDB将列的dataType从文本更改为int
EN

Stack Overflow用户
提问于 2022-03-06 05:42:22
回答 1查看 165关注 0票数 0

我有我的数据库预先创建,它会被加载到应用程序文档目录在最初的启动。我使用DB浏览器进行SQLite

在数据库中,我有一个带有y列的表x。列dataType设置为接受文本,目前是空的。列在那里,但在应用程序的早期版本中没有使用。

我想开始使用该列,但我希望它保存一个Int而不是文本。

如何更改GRDB中列的DataType?

代码语言:javascript
复制
// 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
    }
EN

回答 1

Stack Overflow用户

发布于 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上记录的说明

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71367945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档