Use of MySQL database in Golang

Source: Internet
Author: User

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 ()
    • Working with databases
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.