db,err := sql.Open("mysql","root:123456@tcp(127.0.0.1:3306)/test?charset=utf8")
if err != nil{
fmt.Printf("connect mysql fail ! [%s]",err)
}else{
fmt.Println("connect to mysql success")
}
2.增加
result,err := db.Exec("INSERT INTO person(height,address,head)VALUES (?,?,?)","8","8","8")
if err != nil{
fmt.Println("insert failed,",err)
}
userId,err:= result.LastInsertId()
rowCount,err:=result.RowsAffected() //影响行
fmt.Println("user_id:",userId)
fmt.Println("rowCount:",rowCount)
rowsQuery,err := db.Query("SELECT * FROM person WHERE )
if err != nil{
fmt.Println("select db failed,err:",err)
return
}
for rowsQuery.Next() {
var height int
var head string
var address string
err = rowsQuery.Scan(&head,&height,&address)// Scan 方法会从输入端读取数据并将处理结果存入接收端,接收端必须是有效的指针。
if err != nil {
fmt.Println(err)
return
}
fmt.Println(height,address,head)
}
rowsQuery.Close()
}
4删除
rowsDelete,err:=db.Exec("DELETE FROM person WHERE )
5改
rowsUpdate,err:=db.Exec("UPDATE person SET address=0 WHERE )
代码附录
package main
import (
"database/sql"
"fmt"
_ "github.com/Go-SQL-Driver/MySQL"
)
func main() {
/* db,err :=sql.Open("mysql","root:123456@tcp(127.0.0.1:3306)/person?charset=utf8")*/
db,err := sql.Open("mysql","root:123456@tcp(127.0.0.1:3306)/test?charset=utf8")
if err != nil{
fmt.Printf("connect mysql fail ! [%s]",err)
}else{
fmt.Println("connect to mysql success")
}
/*result,err := db.Exec("INSERT INTO person(height,address,head)VALUES (?,?,?)","8","8","8")
if err != nil{
fmt.Println("insert failed,",err)
}
userId,err:= result.LastInsertId()
rowCount,err:=result.RowsAffected()
fmt.Println("user_id:",userId)
fmt.Println("rowCount:",rowCount)*/
/*rowsQuery,err := db.Query("SELECT * FROM person WHERE )
if err != nil{
fmt.Println("select db failed,err:",err)
return
}
for rowsQuery.Next() {
var height int
var head string
var address string
err = rowsQuery.Scan(&head,&height,&address)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(height,address,head)
}
rowsQuery.Close()*/
/* rowsDelete,err:=db.Exec("DELETE FROM person WHERE )
fmt.Println(rowsDelete.LastInsertId())*/
rowsUpdate,err:=db.Exec("UPDATE person SET address=0 WHERE )
fmt.Print(
rowsUpdate.LastInsertId())
}
postman发送POST请求往数据库增加数据
mysql.go
import (
"database/sql"
"log"
_ "github.com/Go-SQL-Driver/MySQL"
)
type User struct {
Id int `json:"id" form:"id"`
Username string `json:"username" form:"username"`
Password string `json:"password" form:"password"`
}
func DBConn()(db *sql.DB) {
dbDriver := "mysql"
dbUser := "root"
dbPass := "123456"
dbName := "test"
db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
if err != nil { panic(err.Error()) }
return db }
func add(user User) (Id int, err error) {
//1.操作数据库
db:=DBConn()
//推迟数据库连接的关闭
defer db.Close()
stmt, err := db.Prepare("INSERT INTO person(id,username, password) VALUES (?,?, ?)")
if err
//将id类型转换
Id = int(id)
defer stmt.Close()
return
main.go
router.POST("/add", func(c *gin.Context) {
var u User
err := c.Bind(&u)
if err != nil {
log.Fatal(err)
}
Id, err := add(u)
fmt.Print("id=", Id)
c.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("%s 插入成功", u.Username),
})
})