mysql insert update delete finished
parent
6f19983df9
commit
65a48c079f
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="file://$PROJECT_DIR$/mysql/mysql_insert.go" dialect="GenericSQL" />
|
||||
<file url="file://$PROJECT_DIR$/mysql/mysql_rowquery.go" dialect="GenericSQL" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,91 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"log"
|
||||
)
|
||||
|
||||
var database *sql.DB
|
||||
|
||||
func initDataBase() (err error) {
|
||||
database, err = sql.Open("mysql", "root:123456@tcp(43.143.245.135:3306)/wangaodev")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = database.Ping()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Println("Successfully connected to mysql")
|
||||
database.SetMaxOpenConns(10)
|
||||
database.SetMaxIdleConns(5)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 插入数据
|
||||
func insert(name, level string, status int) {
|
||||
//1.写sql语句
|
||||
sqlStr := `insert into user (name,level,status) values (?,?,?)`
|
||||
//2.exec
|
||||
ret, err := database.Exec(sqlStr, name, level, status)
|
||||
if err != nil {
|
||||
log.Println("insert error:", err)
|
||||
return
|
||||
}
|
||||
//如果是插入数据的操作,能够拿到插入数据的id
|
||||
id, err := ret.LastInsertId()
|
||||
if err != nil {
|
||||
log.Println("get id failed:", err)
|
||||
return
|
||||
}
|
||||
log.Println("id:", id)
|
||||
|
||||
}
|
||||
|
||||
// 更新数据
|
||||
func update(name string, id int) {
|
||||
//1.写sql语句
|
||||
sqlStr := `update user set name=? where id=?`
|
||||
//2.exec
|
||||
ret, err := database.Exec(sqlStr, name, id)
|
||||
if err != nil {
|
||||
log.Println("insert error:", err)
|
||||
return
|
||||
}
|
||||
//如果是插入数据的操作,能够拿到插入数据的id
|
||||
affectedNum, err := ret.RowsAffected()
|
||||
if err != nil {
|
||||
log.Println("update row failed:", err)
|
||||
return
|
||||
}
|
||||
log.Println("affectedNum:", affectedNum)
|
||||
|
||||
}
|
||||
|
||||
//删除数据
|
||||
|
||||
func deleteRow(id int) {
|
||||
sqlStr := `delete from user where id=?`
|
||||
ret, err := database.Exec(sqlStr, id)
|
||||
if err != nil {
|
||||
log.Println("delete error:", err)
|
||||
}
|
||||
rowsAffected, err := ret.RowsAffected()
|
||||
if err != nil {
|
||||
log.Println("delete row failed:", err)
|
||||
}
|
||||
log.Println("rowsAffected:", rowsAffected)
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := initDataBase()
|
||||
if err != nil {
|
||||
log.Fatalf("connect mysql failed,error:%v", err)
|
||||
return
|
||||
}
|
||||
//insert("尹晨旭", "L2", 0)
|
||||
//update("吴斌", 19)
|
||||
deleteRow(19)
|
||||
|
||||
}
|
|
@ -36,19 +36,42 @@ func initDb() (err error) {
|
|||
type user struct {
|
||||
id int
|
||||
name string
|
||||
age int
|
||||
level string
|
||||
status int
|
||||
}
|
||||
|
||||
// 查询单行数据
|
||||
func queryRow(id int) {
|
||||
var u user
|
||||
//查询单行记录的sql语句
|
||||
sqlstr := "select id, name, age from user where id=?"
|
||||
sqlstr := "select id,name,level,status from user where id=?"
|
||||
//执行并且拿到结果
|
||||
db.QueryRow(sqlstr, id).Scan(&u.id, &u.name, &u.age) //从链接池中拿一个连接出来去数据库查询单条记录,scan方法自动释放连接,也就是必须调用scan方法
|
||||
db.QueryRow(sqlstr, id).Scan(&u.id, &u.name, &u.level, &u.status) //从链接池中拿一个连接出来去数据库查询单条记录,scan方法自动释放连接,也就是必须调用scan方法
|
||||
fmt.Printf("user:%#v\n", u)
|
||||
}
|
||||
|
||||
// 查询多行数据
|
||||
func queryMultiRow(level string) error {
|
||||
sqlstr := "select id,name,level,status from user where level=?"
|
||||
rows, err := db.Query(sqlstr, level)
|
||||
if err != nil {
|
||||
log.Printf("query multiRow err %v\n", err)
|
||||
return err
|
||||
}
|
||||
//非常重要.关闭rows持有的数据库连接
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var u user
|
||||
err = rows.Scan(&u.id, &u.name, &u.level, &u.status)
|
||||
if err != nil {
|
||||
log.Printf("scan err %v\n", err)
|
||||
return err
|
||||
}
|
||||
fmt.Printf("user:%#v\n", u)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// go链接mysql示例
|
||||
func main() {
|
||||
err := initDb()
|
||||
|
@ -57,4 +80,5 @@ func main() {
|
|||
}
|
||||
log.Println("init DB success.")
|
||||
queryRow(2)
|
||||
queryMultiRow("L1")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue