增加
首先看数据库的结构:
MysqL> desc person; +----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+
| user_id | int(255) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | | NULL | |
| sex | varchar(255) | YES | | NULL | |
| email | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+----------------+
4 rows in set (0.05 sec)
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:root@tcp(127.0.0.1:3306)/safly")
if err != nil {
fmt.Println("open MysqL Failed,",err)
return
}
Db = database
}
func main() {
r,err := Db.Exec("insert into person(username,sex,email)values(?,?,?)","stu001","man","stu01@qq.com")
if err != nil {
fmt.Println("exec Failed,err)
return
}
id,err := r.LastInsertId()
if err != nil {
fmt.Println("exec Failed,err)
return
}
fmt.Println("insert succ:",id)
}
MysqL> select * from person; +---------+----------+------+--------------+
| user_id | username | sex | email | +---------+----------+------+--------------+
| 1 | stu001 | man | stu01@qq.com | +---------+----------+------+--------------+
1 row in set (0.00 sec)
查询
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)
return
}
Db = database
}
func main() {
var person []Person
err := Db.Select(&person,"select user_id,username,email from person where user_id=?", 1)
if err != nil {
fmt.Println("exec Failed,err)
return
}
fmt.Println("select succ:",person)
}
输出如下:
PS E:\golang\go_pro\src\safly> go run demo.go
select succ: [{1 stu001 man stu01@qq.com}]
PS E:\golang\go_pro\src\safly>
修改
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)
return
}
Db = database
}
func main() {
_,err := Db.Exec("update person set username=? where user_id=?","stu0001",err)
return
}
}
MysqL> select * from person; +---------+----------+------+--------------+
| user_id | username | sex | email | +---------+----------+------+--------------+
| 1 | stu0001 | man | stu01@qq.com | +---------+----------+------+--------------+
1 row in set (0.00 sec)
删除
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,"root:root@tcp(127.0.0.1:3306)/safly")
if err != nil {
fmt.Println("open MysqL Failed,err := Db.Exec("delete from person where user_id=?",err)
return
}
fmt.Println("delete succ")
}
输出如下:
MysqL> select * from person;
Empty set (0.00 sec)
原文链接:https://www.f2er.com/go/187689.html