dongxian7489 2019-01-08 11:49
浏览 18
已采纳

如何插入多行

I am actually very new to Go so wanted to know the best way to insert data like this

{
    "moduleId":"M101",
    "topicId":["tt","ee"]
}

in MySQL database using Go

type TopicModule struct {
    ModuleId string   `json:"moduleId" bson:"moduleId" form:"moduleId"`
    TopicId  []string `json:"topicId" bson:"topicId" form:"topicId"`
    AddedBy  string   `json:"addedBy" bson:"addedBy" form:"addedBy"`
}

func AddTopicModuleHandler(ctx iris.Context) {
    topicmodule := new(TopicModule)
    if err := ctx.ReadJSON(topicmodule); err != nil {
        panic(err)
        ctx.StatusCode(400)
        return
    }
    log.Println(topicmodule.TopicId)
    code, created := AddTopicModule(*topicmodule)

    if created {
        ctx.JSON(topicmodule)
        ctx.Redirect("/api/module/"+code, iris.StatusCreated)
    }
}

func AddTopicModule(atm TopicModule) (string, bool) {

    log.Println("the topic is ", atm.TopicId)
    db := DatabaseAccess()
    tx, _ := db.Begin()
    stmt, err := tx.Prepare("insert into ModuleTopic(module_id, topic_id, added_by) Values(?,?,?) ")
    res, err := stmt.Exec(atm.ModuleId, "Ricky")
    res1, err := stmt.Exec(atm.ModuleId, "Ric")

    if err != nil {
        tx.Rollback()
    }
    tx.Commit()
    log.Println(res, res1)
    return "aa", true
}

The expected result is to add the array of JSON into MySQL.

  • 写回答

1条回答 默认 最新

  • duanchi8836 2019-01-08 13:44
    关注

    You can not simply insert an array into the database. Instead, you should loop around the TopicId and insert them one by one

    func AddTopicModule(atm TopicModule) (string, bool) {
        log.Println("the topic is ", atm.TopicId)
        db := DatabaseAccess()
        tx, _ := db.Begin()
        for _, value = range(atm.TopicId){
            stmt, err := tx.Prepare("insert into ModuleTopic(module_id, topic_id, added_by) Values(?,?,?) ")
            if err != nil {
                return "", false
            }
            res, err := stmt.Exec(atm.ModuleId, value, "Harsh")
            if err != nil {
                tx.Rollback()
                return "", false
            }
            tx.Commit()
        }
        return "aa", true
    }
    

    This will create 2 entries in the database for the JSON provided by you.

    |---------------------|------------------|------------------|
    |      module_id      |     topic_id     |     added_by     |
    |---------------------|------------------|------------------|
    |          M101       |         tt       |     Harsh        |
    |---------------------|------------------|------------------|
    |          M101       |         ee       |     Harsh        |
    |---------------------|------------------|------------------|
    

    To get them, simply query your database:

    SELECT * FROM ModuleTopic WHERE module_id = M101;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝