--- Go Go Advanced Mysql database operations

Installation dependencies

Go to operate using two main use mysql package

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

The first package is the Go comes with a database package. But only the first one is not enough, there are many databases, although similar, but there are differences. This time we need to use the Mysql, so drivers need to be able Myqsl a package.
The second package is used to Mysql driver package, the package is not in the standard library Go inside, you need to download.
It can be installed by the following command:

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

If more instructions not download, you can go directly to github.com/go-sql-driver/mysql download, remember in extracting installation (GoPath / src / vendor, and to ensure that the package directory as above, if not the same modification remember) directory.

Then need to import anonymous, I'll update on anonymous package in another blog post.


Database operations

Db define a global variable to represent the database. Then we have two steps:

Database initialization (connection database)
// 定义一个全局对象db
var db *sql.DB

// 定义一个初始化数据库的函数,也就是连接数据库
func initDB() (err error) {
	// DSN:Data Source Name
	//dsn := "user:password@tcp(127.0.0.1:3306)/dbname" // 一般语句
	dsn := "root:123456@tcp(127.0.0.1:3306)/runoob"  // 这是我电脑上的数据库,请根据实际情况自行修改
	// 不会校验账号密码是否正确
	// 注意!!!这里不要使用:=,我们是给全局变量赋值,然后在main函数中使用全局变量db
	db, err = sql.Open("mysql", dsn)
	if err != nil {
		return err
	}
	// 尝试与数据库建立连接(校验dsn是否正确)
	err = db.Ping()
	if err != nil {
		return err
	}
	return nil
}

Operation of the database:

func main() {
	err := initDB() // 调用输出化数据库的函数
	if err != nil {
		fmt.Printf("init db failed,err:%v\n", err)
		return
	}
	// 将runoob1_tb1 这个表中的数据全部读出来
	rows, err := db.Query("SELECT * FROM runoob_tb1")

	// 也可以使用条件选择
	//id := 2
	//rows, err := db.Query("SELECT * FROM runoob_tb1 WHERE id=?", id) //

	//
	if err != nil {
		log.Fatalln(err)
	}
	defer rows.Close()

	for rows.Next() {
		// 取多少个变量就定义多少个变量
		var id int
		var author string
		var title string
		var date string
		if err := rows.Scan(&id, &title, &author, &date); err != nil {
			log.Fatalln(err)
		}
		fmt.Println(id, title, author, date)
	}
	if err := rows.Err(); err != nil {
		log.Fatalln(err)
	}
}

operation result

D:\编程语言和环境\Go\src\studygo\day07\mysql\demo1>go run main.go
1 学习PHP 菜鸡教程 2020-02-13
2 学习MYSQL 菜鸟教程 2020-02-13
3 学习JAVA RUNOOB.COM 2020-02-13
5 学习C++ RUNOOB.COM 2020-02-13
7 学习go kyda 2020-02-16

The results look console:
Here Insert Picture Description
well, as we explain the operation was successful. Some other is much the same. Only minor modifications to the program on top.

This paper reference: https: //www.liwenzhou.com/posts/Go/go_mysql/

If you think I write can, then Here Insert Picture Descriptionfocus on my public number:

Published 67 original articles · won praise 77 · Views 150,000 +

Guess you like

Origin blog.csdn.net/weixin_37720172/article/details/104339885