UPDATE SQL 命令不适用于 GO 语言

在下面的这个例子中,UPDATE命令不起作用。为什么 ?


详见updateDate方法。


package main


import (

    "database/sql"

    "fmt"

    "log"


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

)


type ConnData struct {

    // para usar conexão via socket veja '/etc/mysql/my.cnf'

    // para usar conexão via tcp confira a porta com 'ps -ef | grep mysqld'

    dbuser, dbpasswd, dbname, tcp, socket string

}


var myConnData = ConnData{dbuser: "soma", dbpasswd: "secret1000",

    dbname: "test",

    tcp:    "tcp(127.0.0.1:3307)",

    socket: "unix(/tmp/mysql.sock)"}


// CREATE TABLE userinfo

// (

//     uid serial NOT NULL,

//     username character varying(20) NOT NULL,

//     depto character varying(10)  NULL,

//     Created date,

//     CONSTRAINT userinfo_pkey PRIMARY KEY (uid)

// )

func main() {

    _ = ConnectToMySQLAndRunCmd(myConnData).(string)

}

func ConnectToMySQLAndRunCmd(connData ConnData) interface{} {

    myConnStr := connData.dbuser + ":" + connData.dbpasswd +

        "@" + connData.tcp + "/" + connData.dbname

    log.Printf("Connecting to MySQL Server using %s\n", myConnStr)

    db, err := sql.Open("mysql", myConnStr)

    // myConnStr := connData.dbuser + ":" + connData.dbpasswd +

    //  "@" + connData.tcp + "/" + connData.dbname


    defer db.Close()

    checkErr(err)

    // Open doesn't open a connection. Validate DSN data:

    err = db.Ping()

    checkErr(err)

    // Limpando a tabela

    _, err = db.Exec("DELETE FROM userinfo")

    checkErr(err)

}

出了什么问题?


当我运行主程序时,请参见下面的结果。


$ ./main

2015/09/05 14:04:34 Connecting to MySQL Server using soma:soma@tcp(127.0.0.1:3307)/test

2015/09/05 14:04:34 ID = 3, affected = 1

2015/09/05 14:04:34 3 Pedro TI 2015-09-01

2015/09/05 14:04:34 •••• Updating record 3

Records affected 0

2015/09/05 14:04:34 3 Pedro TI 2015-09-01

我正在使用go build -a -installsuffix cgo -o main .构建主程序


翻翻过去那场雪
浏览 159回答 1
1回答

HUWWW

您似乎颠倒了 lastId 和“2015-09-02”的顺序:cmd := "UPDATE userinfo SET created = ? WHERE uid = ?"res, err := updateDate.Exec(lastId, "2015-09-02")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go