douzhenzu0247 2018-11-25 11:00
浏览 67

如何使用SQL模式更改表?

I'm using sqlx to connect my MySQL database to Golang. I've got an SQL schema where all the CREATE TABLE IF NOT EXISTS... statements are defined. Here an example:

CREATE TABLE IF NOT EXISTS `users`
  (
     `id` INT(0) NOT NULL auto_increment,
     `language_code` VARCHAR(5) NOT NULL,
     `premium` BIT(0) DEFAULT 0,
     `status` VARCHAR(100) NOT NULL DEFAULT '',
     `user_id` INT(0) NOT NULL,
     PRIMARY KEY (id)
  );
...

Here how I connect to database and I execute all the queries everytime:

Database, err = sqlx.Connect(Config.Database.Type, Config.Database.DataSourceString) // Connect to the database

if err != nil { // If there's an error, handle it
    panic(err.Error())
}

defer Database.Close() // Close the database when the function finishes

schemaSQL, err := ioutil.ReadFile("schema.sql") // Read the configuration file (DealsFinderBot/config.yaml)

if err != nil { // If there's an error, handle it
    panic(err.Error())
}

for _, query := range strings.Split(string(schemaSQL), ";") { // Loop on the queries of the schema
    if strings.TrimSpace(query) != "" { // If the query is not empty
        Database.MustExec(query) // Execute the query
    }
}

I would like to update every time (alter a table) everytime I add a new column or I edit some parameters (like NOT NULL or DEFAULT ''). How could I do this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 mmocr的训练错误,结果全为0
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀