go语言基础 链接MySQL

插入操作:
step1:导包
"database/sql"
_ "github.com/go-sql-driver/mysql"
step2:打开数据库,建立连接
db,_:=sql.Open("mysql","root:xxx@tcp(127.0.0.1:3306)/my1802?charset=utf8") // xxx是密码
step3:操作数据库:
stmt_:=db.Prepare("sql .... ?,?")
result,_:=stmt.Exec(给?赋值)
step4:操作结果对象
result.LastIndexId()-->int64
result.RowsAffected()-->int64
step5:关闭资源
stmt.Close()

db.Close()

package main
// step1:导入包
import (
   "database/sql"
   _"github.com/go-sql-driver/mysql"
   "fmt"
)

func main()  {
   // step2:打开数据库,相当于和数据库建立连接:db对象
   /*
   func Open(driverName, dataSourceName string) (*DB, error)
   drvierName,"mysql"
   dataSourceName,用户名:密码@协议(地址:端口)/数据库?参数=参数值
    */
   db,err:=sql.Open("mysql","root:hanru1314@tcp(127.0.0.1:3306)/my1802?charset=utf8")
   if err !=nil{
      fmt.Println("连接失败。。")
      return
   }
   //step3:插入一条数据

   stmt,err:=db.Prepare("INSERT INTO emp(empno,ename,job,hiredate,sal) values(?,?,?,?,?)")
   if err !=nil{
      fmt.Println("操作失败。。")
   }
   //补充完整sql语句,并执行
   result,err:=stmt.Exec(9530,"小钻风","巡山","2018-04-21",30.8)
   if err !=nil{
      fmt.Println("插入数据失败。。")
   }
   //step4:处理sql操作后的结果
   lastInsertId,err:=result.LastInsertId()
   rowsAffected,err:=result.RowsAffected()
   fmt.Println("lastInsertId",lastInsertId)
   fmt.Println("影响的行数:", rowsAffected)


   //再次插入数据:
   result,_=stmt.Exec(9531,"白骨精","巡山","2017-11-11",50.8)
   count,_:=result.RowsAffected()
   fmt.Println("影响的行数:",count)




   //step5:关闭资源
   stmt.Close()
   db.Close()


}

查询数据库:DQL语言
step1:同上,导入包
step2:同上,建立连接
step3:查询
rows,err:=db.Query("select....",占位符的替换的值)
step4:处理查询到的数据
rows.Colums()-->查询到的字段的名字 []string
rows.Next()-->bool,判断是否有下一个数值
rows.Scan(&变量名,&....)

package main
//step1:导入包
import (
   "database/sql"
   _ "github.com/go-sql-driver/mysql"
   "fmt"
)
type Emp struct {
   Empno int
   Ename string
   Job string
   Hiredate string
   Sal float64
   Deptno int
}

func main()  {
   /*
   查询操作:
    */
   //step2:打开数据库,建立连接
   db,_ := sql.Open("mysql","root:hanru1314@tcp(127.0.0.1:3306)/my1802?charset=utf8")

   //stpt3:查询数据库
   rows,err:=db.Query("SELECT empno,ename,job,hiredate,sal,deptno FROM emp WHERE deptno=?",30)
   fmt.Println("--->",err.Error())
   fmt.Println(rows.Columns()) //[empno ename job hiredate sal deptno]
   //思路一:定义一个map,用于存储从数据库中查询出来的数据,字段作为key,string,数据作为value,任意类型,空接口
   map1:=make(map[string]interface{})
   datas := make([] map[string]interface{},0)

   //思路二:创建slice,存入struct,
   datas2:=make([] Emp,0)
   //step4:操作结果集获取数据
   for rows.Next(){
      var empno int
      var ename string
      var job string
      var hiredate string
      var sal float64
      var deptno int
      if err:=rows.Scan(&empno,&ename,&job,&hiredate,&sal,&deptno);err!=nil{
         fmt.Println("获取失败。。")
      }
      //fmt.Println(empno,ename,job,hiredate,sal,deptno)
      //将读取到的数据,存入了map中
      map1["empno"]=empno
      map1["ename"]=ename
      map1["job"]=job
      map1["hiredate"]=hiredate
      map1["sal"]=sal
      map1["deptno"]=deptno
      //将map存入切片中
      datas = append(datas,map1)


      //思路二:每读取一行,创建一个emp对象,存入datas2中
      emp := Emp{empno,ename,job,hiredate,sal,deptno}
      datas2 =append(datas2, emp)
   }
   //step5:关闭资源
   rows.Close()
   db.Close()

   //遍历切片
   //fmt.Println("empno\tename\tjob\thiredate\tsal\tdeptno")
   //for _,v:=range datas{
   // for _,val:=range v{
   //    fmt.Print(val,"\t")
   //}
   //fmt.Println()
   //
   //}

   for _,v:=range  datas2{
      fmt.Println(v)
   }


}


你可能感兴趣的:(数据库)