1. Package used by Go connection database
"Database/sql" "Github.com/go-sql-driver/mysql"
2. Go to connect to a database
- The first is to open the database
return value: 1. *sql. DB Database Connection index 2. Error code SQL. Open ()//close the database and use the index of the Open database for the database shutdown operation (*sql. DB). Close ()
database query and processing work (take a query as an example) rows, Err:=query (sqllanguage)//Close the query cursor defer rows. Close ()//Gets the column name of the data table columns,err:=rows. Columns ()//defines the data to be processed scans:=make ([]interface{},len (Columns))//loops through each line until the end for rows. Next () {//reads the contents of a line and puts the contents into a variable//the type to be read to is a byte to be displayed it needs to be converted to a string form by using string ([]byte). Scan (Scans ...)}
- Additional Operations for the database
Additions, modifications, and deletions in the data are using the EXEC () function
You can use PrePare to prepare the statement//prepare that the operation executes (Sqllanguage where the parameter is represented with a question mark)//And then execute exec (parameter) to perform the associated function sql,err:=db. PrePare () Res,err:=sql. Exec ()
Reference Learning article Address: 1190000003036452
Use of MySQL database in Golang