Preface Golang provides the database/sql package for accessing SQL databases. As the entry object for operating the database, sql.DB mainly provides us with two important functions: •sql.DB provides us with the ability to manage the opening and closing of underlying database connections through the database driver. •sql.DB manages the database connection pool for us It should be noted that sql.DB represents an abstract access interface for operating the database, rather than a database connection object; it can open and close database connections and manage connection pools based on the driver. The connection in use is marked as busy and returns to the connection pool after use to wait for the next use. Therefore, if you do not release the connection back to the connection pool, it will cause too many connections to exhaust system resources. Introduction to MySQL operation with Golang The feeling of operating MySQL database in Golang is a bit like the operation of MySQL in PDO in PHP. If you are originally a PHPer, you will find it very familiar to switch to Golang. The overall feeling is very simple Notes on Golang's operation of MySQL Golang implements the standard library for MySQL operations but does not implement the MySQL driver Therefore, we need to download the go get github.com/go-sql-driver/mysql Create a table field in the test database as follows CREATE TABLE IF NOT EXISTS `test`.`user` ( `user_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'User ID', `user_name` VARCHAR(45) NOT NULL COMMENT 'User name', `user_age` TINYINT(3) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'User age', `user_sex` TINYINT(3) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'User gender', PRIMARY KEY (`user_id`)) ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 'User table' Implementing the insert operation on data package main import ( "fmt" "database/sql" //Import mysql driver_ "github.com/go-sql-driver/mysql" ) func main(){ //Use Open in the database/sql package to connect to the database db, err := sql.Open("mysql","root:root@tcp(localhost:3306)/test?charset=utf8") if err != nil { fmt.Println("Failed to connect to database:",err) return } //Use the DB structure instance method Prepare to preprocess the insert. Prepare will return a stmt object stmt, err := db.Prepare("insert into `user`(user_name,user_age,user_sex)values(?,?,?)") if err!=nil{ fmt.Println("Preprocessing failed:",err) return } //Use the Stmt object to execute the preprocessing parameters result,err := stmt.Exec("pengjin",33,"男") if err!=nil{ fmt.Println("Failed to execute preprocessing:",err) return }else{ rows,_ := result.RowsAffected() fmt.Println("Execution successful, number of affected rows", rows,"rows") } } The above code gives a feeling of operating pdo in PHP. In fact, the above code can also be implemented directly through the Exec method of the Stmt instance without writing the Prepare method. Summarize The above is the implementation code for operating MySQL database in golang introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website! You may also be interested in:
|
<<: Using JS timer to move elements
>>: A record of a Linux server intrusion emergency response (summary)
Using CI to build docker images for release has g...
This tutorial shares the process of manually inst...
Table of contents Why use gzip compression? nginx...
When coding, you will find that many things have ...
I won't go into details about how important b...
Table of contents Preface What is data binding? T...
Implementation principle The main graphics are co...
Today I received a disk warning notification from...
Mysql5.7.19 version is a new version launched thi...
I want to make a docker for cron scheduled tasks ...
VUE uses vue-seamless-scroll to automatically scr...
1. Yum installation yum install subversion 2. Con...
This article uses an example to describe the erro...
Object.defineProperty Understanding grammar: Obje...
Table of contents Starting from type judgment Str...