package main
import (
"encoding/json"
"fmt"
"time"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
type Users struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Created_At *string `json:"created_at"`
Updated_At *string `json:"updated_at"`
}
// create
func create() {
db, err := sql.Open("mysql", "root:root@/go_sql")
if err != nil {
fmt.Println("create connetion failed.....", err)
return
}
defer db.Close()
sql := "insert into users(name,age,created_at)values(?,?,?)"
stmt, _ := db.Prepare(sql)
defer stmt.Close()
rs, err := stmt.Exec("BOB", 12, GetTime())
if err != nil {
fmt.Println("create info faield.......", err)
return
}
id, _ := rs.LastInsertId()
fmt.Println("last_insert_id value is: ", id)
affert_num, _ := rs.RowsAffected()
fmt.Println("number of affcted rows", affert_num)
}
// edit
func edit() {
db, err := sql.Open("mysql", "root:root@/go_sql")
if err != nil {
fmt.Println("create connetion failed.....", err)
return
}
defer db.Close()
sql := "update users set name=?,updated_at=? where id=?"
stmt, _ := db.Prepare(sql)
defer stmt.Close()
rs, err := stmt.Exec("Allent", GetTime(), 1)
if err != nil {
fmt.Println("edit faield.....", err)
return
}
affert_num, _ := rs.RowsAffected()
fmt.Println(affert_num)
}
// delete
func delete() {
db, err := sql.Open("mysql", "root:root@/go_sql")
if err != nil {
fmt.Println("create connetion failed.....", err)
return
}
defer db.Close()
sql := "delete from users where id=?"
stmt, _ := db.Prepare(sql)
defer stmt.Close()
rs, _ := stmt.Exec(1)
affert_num, _ := rs.RowsAffected()
fmt.Println(affert_num)
}
// one query
func query() {
db, err := sql.Open("mysql", "root:root@/go_sql")
if err != nil {
fmt.Println("create connetion failed.....", err)
return
}
defer db.Close()
var user Users
db.QueryRow("select * from users where id=?", 3).Scan(&user.ID, &user.Name, &user.Age, &user.Created_At, &user.Updated_At)
rs, _ := json.Marshal(user)
fmt.Println(string(rs))
}
// more query
func more_query() {
db, err := sql.Open("mysql", "root:root@/go_sql")
if err != nil {
fmt.Println("create connetion failed.....", err)
return
}
defer db.Close()
rows, err := db.Query("select * from users")
if err != nil {
fmt.Println("more query faield......", err)
return
}
defer rows.Close()
var user Users
users := make([]Users, 0)
for rows.Next() {
err := rows.Scan(&user.ID, &user.Name, &user.Age, &user.Created_At, &user.Updated_At)
if err != nil {
fmt.Println("row.scan failed......", err)
return
}
users = append(users, user)
}
rs, _ := json.Marshal(users)
fmt.Println(string(rs))
}
func main() {
create()
edit()
delete()
query()
more_query()
}
func GetTime() string {
return time.Now().Format("2006-01-02 15:04:05")
}
GO 操作数据库 CURD
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 转载请注明出处:https://www.jianshu.com/p/664ddc66858f 1、数据库的简介 2...
- 之前我在搞网站的时候,因为一些数据库的表存在不同的服务器,所以就要把表结合起来,如果文件大的话,倒入导出就要等很久...