1. 程式人生 > >Go語言之進階篇連接mysql

Go語言之進階篇連接mysql

nbsp rac 信息 連接mysql field 連接 drive perf 自動下載

一、Go連接mysql

1、mysql驅動

地址:https://github.com/Go-SQL-Driver/MySQL

說明:

sql.Open()函數用來打開一個註冊過的數據庫驅動,Go-MySQL-Driver中註冊了 mysql這個數據庫驅動,第二個參數是DNS,它是Go-MySQL-Driver定義的一些數據庫鏈接和配置信息。

2、下載mysql驅動包方法

在LiteIDE中,選中"github.com/go-sql-driver/mysql" 這行代碼,再點編譯--->"Get",等待一會,就會自動下載完成。

代碼:

package main

import (
	"fmt"

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

#在cmd中運行安裝命令
go get github.com/go-sql-driver/mysql 

  

3、創建數據庫

1、創建數據庫
mysql> create database nulige character set utf8;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nulige             |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use nulige
Database changed

2、建表
mysql> create table user_info(id int(11), usernmae varchar(20), departname varch
ar(20), create_time timestamp default "0000-00-00 00:00:00", primary key(id));

3、查看表
mysql> show tables;
+------------------+
| Tables_in_nulige |
+------------------+
| user_info        |
+------------------+
1 row in set (0.00 sec)

4、查看表結構
mysql> desc user_info;
+-------------+-------------+------+-----+---------------------+-------+
| Field       | Type        | Null | Key | Default             | Extra |
+-------------+-------------+------+-----+---------------------+-------+
| id          | int(11)     | NO   | PRI | 0                   |       |
| usernmae    | varchar(20) | YES  |     | NULL                |       |
| departname  | varchar(20) | YES  |     | NULL                |       |
| create_time | timestamp   | NO   |     | 0000-00-00 00:00:00 |       |
+-------------+-------------+------+-----+---------------------+-------+
4 rows in set (0.01 sec)

@

Go語言之進階篇連接mysql