还是那句话,服务器嘛,每个数据库支持,那成啥子啦嘛!

好吧,今天,就让Go能连上数据库,当然是之前给你铺垫的MySql的啦,哈哈

一。安装第三方包支持访问mysql数据库

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

二。写一个主程序

//database.go

package main

import (

"database/sql"

"fmt"

_ "github.com/go-sql-driver/mysql"

)

func checkErr(err error) {

if err != nil {

panic(err)

}

}

func main() {

//打开数据库mytest

fmt.Println("open the database, mytest")

db, err := sql.Open("mysql", "root:11111111@tcp(127.0.0.1:3306)/mytest?charset=utf8")

checkErr(err)

//插入数据

fmt.Println("insert data info into table, userinfo")

stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")

checkErr(err)

res, err := stmt.Exec("Test", "people", "2017-10-27")

checkErr(err)

//记录ID

fmt.Println("record the ID of affecting item")

id, err := res.LastInsertId()

checkErr(err)

fmt.Println(id)

  //更新数据

fmt.Println("update data info where id was conform to be affected")

stmt, err = db.Prepare("update userinfo set username=? where uid=?")

checkErr(err)

res, err = stmt.Exec("man", id)

checkErr(err)

affect, err := res.RowsAffected()

checkErr(err)

fmt.Println(affect)

//查询数据

fmt.Println("query data info from table, userinfo")

rows, err := db.Query("SELECT * FROM userinfo")

checkErr(err)

fmt.Println("iterator data info to show")

for rows.Next() {

var uid int

var username string

var department string

var created string

err = rows.Scan(&uid, &username, &department, &created)

checkErr(err)

fmt.Println(uid)

fmt.Println(username)

fmt.Println(department)

fmt.Println(created)

}

  //删除数据

stmt, err = db.Prepare("delete from userinfo where uid=?")

checkErr(err)

res, err = stmt.Exec(id)

checkErr(err)

affect, err = res.RowsAffected()

checkErr(err)

fmt.Println(affect)

//关闭数据库mytest

db.Close()

}

三。建立数据库和表

mysql>create database mytest;

mysql>use mytest;

mysql> CREATE TABLE `userinfo` (

->     `uid` INT(10) NOT NULL AUTO_INCREMENT,

->     `username` VARCHAR(64) NULL DEFAULT NULL,

->     `departname` VARCHAR(64) NULL DEFAULT NULL,

->     `created` DATE NULL DEFAULT NULL,

->     PRIMARY KEY (`uid`)

-> );

四。结果

# ./database

open the database, mytest

insert data info into table, userinfo

record the ID of affecting item

4

update data info where id was conform to be affected

1

query data info from table, userinfo

iterator data info to show

4

man

people

2017-10-27

1

Finally:

哈哈,数据库都给你配上了,诸位还不开始Go(滚)吗?

哈哈哈哈哈哈哈

Golang mysql的更多相关文章

  1. golang mysql 如何设置最大连接数和最大空闲连接数

    本文介绍golang 中连接MySQL时,如何设置最大连接数和最大空闲连接数. 关于最大连接数和最大空闲连接数,是定义在golang标准库中database/sql的. 文中例子连接MySQL用的SQ ...

  2. Golang mysql 上线的一个坑 Db.close重要性

    急冲冲完成的mysql的一个监控自动处理程序上线了,线下处理是正常的,没想到线上才半小时就奔溃了. 现在时间是晚上11点,心慌焦虑涌上心头,需要熬夜?肾上腺素激增. 程序主要是一个定时任务的处理程序, ...

  3. golang mysql 的 packet sequence error 这个错

    在公司用golang 写了个插入外链数据的服务,这服务是2016年写的,大概作用就是,python 爬取的数据,要同步到 wordpress中,golang就负责,将数据整理,图片下载弄到 wordp ...

  4. golang Mysql -- Tx

    Transaction 事务 事务处理是数据的重要特性.尤其是对于一些支付系统,事务保证性对业务逻辑会有重要影响.golang的mysql驱动也封装好了事务相关的操作.我们已经学习了db的Query和 ...

  5. Golang mysql数据库

    基本操作: Open() – create a DB Close() - close the DB Query() - 查询 QueryRow() -查询行 Exec() -执行操作,update,i ...

  6. golang mysql demo

    Go操作Mysql数据库 使用Go操作MySQL等数据库,一般有两种方式:一是使用database/sql接口,直接在代码里硬编码sql语句:二是使用gorm,即对象关系映射的方式在代码里抽象的操作数 ...

  7. golang mysql 模糊查询

    db.SqlDB.Query("SELECT id,name FROM test_table where title name like CONCAT('%',?,'%');", ...

  8. 我的Vue之旅 10 Gin重写后端、实现页面详情页 Mysql + Golang + Gin

    第三期 · 使用 Vue 3.1 + Axios + Golang + Mysql + Gin 实现页面详情页 使用 Gin 框架重写后端 Gin Web Framework (gin-gonic.c ...

  9. 初步认知MySQL metadata lock(MDL)

    http://blog.itpub.net/26515977/viewspace-1208250/ 概述 随着5.5.3引入MDL,更多的Query被“Waiting for table metada ...

随机推荐

  1. spring的自生一个bug

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  2. 转发一篇好文:36氪翻译自medium的文章: 读书没有 KPI:为什么坚持“一年读 100 本书”没用?

    你只是为了达成所谓的数量目标而读书. 编者按:读书本是一项安静.缓慢的活动,但随着现代社会节奏的加快,信息技术的广泛普及,读书这一行为模式也开始发生了变化.越来越多的人开始碎片化阅读,并且越来越多的文 ...

  3. POJ 1063 - Flip and Shift

    Description This puzzle consists of a random sequence of m black disks and n white disks on an oval- ...

  4. SQL Fundamentals: 子查询 || WHERE,HAVING,FROM,SELECT子句中使用子查询,WITH子句

    SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...

  5. JS弹出对话框的三种方式

    JS弹出对话框的三种方式 我们用到了alert()方法.prompt()方法.prompt()方法,都是在网页有一个弹出框,那么就让我们探究一下他们之间的区别: 一.第一种:alert()方法 < ...

  6. Python:多线程

    据廖雪峰老师的学习文档介绍,高级语言通常都内置多线程的支持,Python也不例外,并且,Python的线程是真正的Posix Thread,而不是模拟出来的线程. Python的标准库提供了两个模块: ...

  7. C 缓冲区过读 if (index >= 0 && index < len)

    C 缓冲区过读 if (index >= 0 && index < len) CWE - CWE-126: Buffer Over-read (3.2) http://cw ...

  8. int 4 bytes

    http://waynewhitty.ie/blog-post.php?id=19 MySQL - INT(11) vs BIGINT(11) vs TINYINT(11) This seems to ...

  9. zabbix zatree centos7安装zabbix-agent

    https://github.com/Emersonxuelinux/zatree-3.0-/tree/master/zabbix-3.0.x /bin/sh /config/ds.sh /tmp/z ...

  10. day0315 迭代器

    一. 迭代器 1.什么是可迭代器? 除了数字和布尔值之外,其他数据类型都是可迭代对象.(字符串,列表,元组,字典,集合) 2.可迭代协议 2.1 可以被迭代要满足的要求就叫可迭代协议,可迭代的定义非常 ...