时间:2021-07-01 10:21:17 帮助过:4人阅读
package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) type Person struct { UserId int `db:"user_id"‘` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } type Place struct { Country string `db:"country"` City string `db:"city"` TelCode int `db:"telcode"` } var Db *sqlx.DB func init() { //初始化链接数据库 database, err := sqlx.Open("mysql", "root:@tcp(127.0.0.1:3306)/test") if err != nil { fmt.Println("open mysql failed", err) return } Db = database } func main() { var person []Person err := Db.Select(&person, "select user_id,username,sex,email from person where user_id=?", 1) if err != nil { fmt.Println("exec failed,", err) return } fmt.Println("select succ:", person) }select
package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) type Person struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } type Place struct { Country string `db:"country"` City string `db:"city"` TelCode int `db:"telcode"` } var Db *sqlx.DB func init() { database, err := sqlx.Open("mysql", "root@tcp(127.0.0.1:3306)/test") if err != nil { fmt.Println("open mysql failed,", err) return } Db = database } func main() { _, err := Db.Exec("update person set username=? where user_id=?", "chaoge666", 1) if err != nil { fmt.Println("exec failed,", err) } }update
package main import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) type Person struct { UserId int `db:"user_id"` Username string `db:"username"` Sex string `db:"sex"` Email string `db:"email"` } type Place struct { Country string `db:"country"` City string `db:"city"` TelCode int `db:"telcode"` } var Db *sqlx.DB func init() { //初始化链接数据库 database, err := sqlx.Open("mysql", "root@tcp(127.0.0.1:3306)/test") if err != nil { fmt.Println("open mysql failed,", err) return } Db = database } func main() { _, err := Db.Exec("delete FROM person where user_id=?", 1) if err != nil { fmt.Println("exec failed,", err) return } fmt.Println("delete succ") }delete
Golang之Mysql操作
标签:操作 账号 where eve 增加 36-6 int ret failed