在下面的这个例子中,UPDATE命令不起作用。为什么 ?详见updateDate方法。package mainimport ( "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)}出了什么问题?当我运行主程序时,请参见下面的结果。$ ./main2015/09/05 14:04:34 Connecting to MySQL Server using soma:soma@tcp(127.0.0.1:3307)/test2015/09/05 14:04:34 ID = 3, affected = 12015/09/05 14:04:34 3 Pedro TI 2015-09-012015/09/05 14:04:34 •••• Updating record 3Records affected 02015/09/05 14:04:34 3 Pedro TI 2015-09-01我正在使用go build -a -installsuffix cgo -o main .构建主程序
1 回答
HUWWW
TA贡献1874条经验 获得超12个赞
您似乎颠倒了 lastId 和“2015-09-02”的顺序:
cmd := "UPDATE userinfo SET created = ? WHERE uid = ?"
res, err := updateDate.Exec(lastId, "2015-09-02")
- 1 回答
- 0 关注
- 158 浏览
添加回答
举报
0/150
提交
取消