Home > Backend Development > Golang > Golang mysql operation introduction

Golang mysql operation introduction

Release: 2020-02-26 17:39:25
forward
3045 people have browsed it

Go (also known as Golang) is a statically strongly typed, compiled, concurrent programming language with garbage collection capabilities developed by Google. Below, I will introduce some operations about mysql from the go Getting Started Tutorial column.

Golang mysql operation introduction

go operates the mysql database. The library used is go-sql-driver/mysql. Install directly go get

go get -u github.com/go-sql-driver/mysql
Copy after login

sql.DB

The functions available for DB type are:

1. Query(c string, args interface{}…) (*Rows, error), commonly used in SELECT statements

2. Exec(c string, args interface{}…) (*Rows, error), commonly used with UPDATE and INSERT

3. Prepare(c string) (*Stmt, error), other statements, too It can be used to execute the above statement and return the Stmt pointer

sql.Stmt

Stmt is a prepared statement that can execute database statement operations. Commonly used functions are:

Exec(args interface{} …) (Result, error), given parameters and executes the prepared statement, and then returns the total result of the statement

//...
	stmt, err := db.Perpare("INSERT INTO User(user,pwd) VALUES (?, ?)")
	if err != nil {
		panic(err)
	}
	defer stmt.Close()
	_, err := stmt.Exec("laoli", "123456")
	if err != nil {
		panic(err)
	}
//...
//...
	stmt, err := db.Perpare("DELETE FROM User")
	if err != nil {
		panic(err)
	}
	defer stmt.Close()
	_, err := stmt.Exec()
	if err != nil {
		panic(err)
	}
Copy after login

Query(args interface{} …) ( *Rows, error), given parameters and executes the prepared statement, returning the row results, for example, the SELECT operation must be called with this function.

sql.Rows

Rows is the table returned by sql statement execution. Rows will occupy the cache. In Rows.Next(), if false is returned, it will be automatically released. cache.
Look at the source code:

// 源码
func (rs *Rows) Next() bool {
	var doClose, ok bool
	withLock(rs.closemu.RLocker(), func() {
		doClose, ok = rs.nextLocked()
	})
	if doClose {
		rs.Close() //在这里释放掉了
	}
	return ok
}
Copy after login

Example:

package main

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

var db *sql.DB
func mian() {
	db = sql.Open("mysql", "root:123456@tcp(127.0.0.1)/test")
	if err := db.Ping(); err != nil {
		panic(err)
	}
}

func Insert() {
	_, err := db.Exec("INSERT INTO User(user, pwd) VALUE (?, ?)", "laowang", "123456")
	if err != nil {
		panic(err)
	}
}

func SelectRow() {
	rows, err := db.Query("SELECT * FROM User WHERE user=?", "laowang")
	if err != nil {
		panic(err)
	}
	//defer rows.Close() //如果后面代码没有循环调用rows.Next(),就需要手动在这里释放一下,不然会一直占用缓存
	var user string
	var pwd string
	for rows.Next() {
		row.Scan(&user, &pwd)
	}
	print(user, pwd)
}
Copy after login

For more go language knowledge, please pay attention to the go language tutorial column on the php Chinese website.

The above is the detailed content of Golang mysql operation introduction. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template