hi
我用github.com/Go-SQL-Driver/Mysql操作mysql
例子参照go web 编程教程
增改删按顺序操作没问题,但是只修改就报错,求助大神是什么原因造成的?
报错信息:
.\test.go:47: undefined: stmt
.\test.go:48: undefined: res
.\test.go:48: undefined: stmt
.\test.go:49: undefined: res
//代码如下:
package main
import (
"database/sql"
"fmt"
_ "github.com/Go-SQL-Driver/Mysql"
)
func main() {
db, err := sql.Open("mysql", "root:@/test?charset=utf8")
//checkErr(err, "连接成功")
// stmt, err := db.Prepare("INSERT into userinfo (username, departname, created) values (?,?,?)")
// res, err := stmt.Exec("astaxie", "研发部门", "2012-12-09")
// checkErr(err, "插入成功")
// fmt.Println(res)
stmt, err = db.Prepare("UPDATE userinfo set username=? where uid=?")
res, err = stmt.Exec("astaxieupdate", 12)
affect, err := res.RowsAffected()
checkErr(err, "更新成功")
fmt.Println(affect)
// //查询数据
// rows, err := db.Query("SELECT * FROM userinfo")
// for rows.Next() {
// var uid int
// var username string
// var department string
// var created string
// err = rows.Scan(&uid, &username, &department, &created)
// checkErr(err, "查询")
// fmt.Println(uid)
// fmt.Println(username)
// fmt.Println(department)
// fmt.Println(created)
// }
//删除数据
// stmt, err = db.Prepare("delete from userinfo where uid=?")
// res, err = stmt.Exec(30)
// fmt.Println(res)
// db.Close()
}
func checkErr(err error, msg string) {
if err != nil {
panic(err)
} else {
fmt.Println(msg)
}
}