Golang查询MySQL数据库

栏目: Go · 发布时间: 6年前

内容简介:模块安装代码数据库的数据内容

模块安装

go  get  -u github.com/go-sql-driver/mysql

代码

package main

import (
   "database/sql"
   _"github.com/go-sql-driver/mysql"
   "log"
   "fmt"
)

func  main(){
   db,err :=sql.Open("mysql","root:@tcp(127.0.0.1:3306)/nginxlog") //连接数据库
   checkErr(err)
   rows,err :=db.Query("select  *  from  nginxlog  limit  10; ")
   checkErr(err)
   for rows.Next(){
      var id    int
      var ip    string
      var time  string
      var  methods string
      var  source  string
      var  protocol   string
      var status string
      err = rows.Scan(&id,&ip,&time,&methods,&protocol,&status,&source)
      checkErr(err)
      fmt.Println(id,ip,time,methods,status,source)

   }


}
//校验函数
func  checkErr(err error){
   if  err  != nil{
      log.Println(err)
   }
}

数据库的数据内容

Golang查询 <a href='https://www.codercto.com/topics/18746.html'>MySQL</a> 数据库

Golang查询的结果:

Golang查询MySQL数据库


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Probability and Computing: Randomization and Probabilistic Techn

Probability and Computing: Randomization and Probabilistic Techn

Michael Mitzenmacher、Eli Upfal / Cambridge University Press / 2017-7-3 / USD 62.23

Greatly expanded, this new edition requires only an elementary background in discrete mathematics and offers a comprehensive introduction to the role of randomization and probabilistic techniques in m......一起来看看 《Probability and Computing: Randomization and Probabilistic Techn》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器