dongzhenjian5195 2015-06-04 20:25
浏览 140
已采纳

GoLang MySQL执行无法正常工作

So I'm trying to use a mysql driver to insert data into a database.

Specifically, I'm using this one -

"github.com/go-sql-driver/mysql"

This is my code

func main() {

db, err := sql.Open("mysql", "psanker:123@/education_data")
err = db.Ping()

if err != nil {
    fmt.Println("Failed to prepare connection to database")
    log.Fatal("Error:", err.Error())
}

defer db.Close()


    content, err := ioutil.ReadFile("activities.csv")

    lines := strings.Split(string(content), "")

    //only work so long as I have one district
    rows, err := db.Query("SELECT id FROM districts")
    var districtId int
    defer rows.Close()

    for rows.Next() {
        err := rows.Scan(&districtId)
        check(err)
    }


    for i, line := range lines {
        if i > 1 {
            splitStr := strings.Split(line, ",")
            var activityCode string

            if strings.Contains(splitStr[0], "-") {
                //this is an activity
                activityCode = splitStr[0]
                stmt1, _ := db.Prepare("INSERT INTO activities(code) VALUES(?)")
                stmt2, _ := db.Prepare("INSERT INTO activities(name) VALUES(?)")
                res, _ := stmt1.Exec(splitStr[0])
                stmt2.Exec(splitStr[1])
            } else {
                //this is a sub activity
                stmt1, _ := db.Prepare("INSERT INTO sub_activities(code) VALUES(?)")
                stmt2, _ := db.Prepare("INSERT INTO sub_activities(name) VALUES(?)")
                stmt1.Exec(splitStr[0])
                stmt2.Exec(splitStr[1])
                if activityCode != "" {
                    rows, _ := db.Query("SELECT id from activities where code = ?", activityCode)
                    var activityId int
                    for rows.Next() {
                        err := rows.Scan(&activityId)
                        check(err)
                        stmt3, err := db.Prepare("INSERT INTO sub_activities(activity_id) VALUES(?)")
                        stmt3.Exec(activityId)
                    }
                }
                rows, _ := db.Query("SELECT id from sub_activities where code= ?", splitStr[0])
                var sub_activityId int
                for rows.Next() {
                    err := rows.Scan(&sub_activityId)
                    check(err)
                    stmt5, _ := db.Prepare("INSERT INTO sub_activity_expenditure(district_id) VALUES(?)")
                    stmt6, _ := db.Prepare("INSERT INTO sub_activity_expenditure(sub_activity_id) VALUES(?)")
                    stmt7, _ := db.Prepare("INSERT INTO sub_activity_expenditure(expenditure) VALUES(?)")
                    stmt5.Exec(districtId)
                    stmt6.Exec(sub_activityId)
                    stmt7.Exec(splitStr[2])
                }
            }
        }
    }
}

When I check my sql database, there are no rows inserted into the tables. I think I'm accessing the right database because the initial query that gets id from districts is returning 1, which is correct.

What's going on?

EDIT

I have confirmed I'm in the right db by doing this

rows, err = db.Query("SELECT DATABASE();")
var test string

for rows.Next() {
    rows.Scan(&test)
    fmt.Println(test)
}

Which prints out education_data

  • 写回答

1条回答 默认 最新

  • dsgwii4867 2015-06-04 20:44
    关注

    As you are trying to insert into a table, I was reminded (in chat at least) that your columns are non-null and you really meant to create one row when you were in fact creating two. It failed on stmt1 and stmt2 because non-nullable columns were not given parameters to replace for all necessary data for insertion to succeed.

    Focus on creating one row for stmt1 and stmt2 (combine them into a single stmt1 to execute with 2 parameters).

    Make sure strings are wrapped with a single quote.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制