19. Note Go language--using MySQL driver

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

19. Note Go language--using MySQL driver

MySQL Driver

Go in support of MySQL driver is more, there are several, some are support database/sql standard, and some are using their own implementation interface, as follows:

Https://github.com/Go-SQL-Driver/MySQL

Download the Mysql-master.zip package and unzip the desired go file.

Creating libraries and tables

Create DATABASE test;

CREATE TABLE ' UserInfo ' (

' UID ' INT (Ten) not NULL auto_increment,

' Username ' VARCHAR (+) null DEFAULT null,

' Departname ' VARCHAR (+) null DEFAULT null,

' Created ' DATE null DEFAULT null,

PRIMARY KEY (' uid ')

);

CREATE TABLE ' Userdetail ' (

' UID ' INT (Ten) not NULL DEFAULT ' 0 ',

' Intro ' TEXT NULL,

' Profile ' TEXT NULL,

PRIMARY KEY (' uid ')

);

Create user

CREATE USER ' astaxie ' @ ' localhost ' identified by ' Astaxie ";

Mysql> Grant all privileges on * * to astaxie@localhost identified by ' Astaxie ';

File

Create Go-sql-driver\mysql in the Github.com folder

The downloaded file is copied to the created Go-sql-driver\mysql folder.

Code

Package Main

Import (

"Database/sql"

"FMT"

_ "Github.com/go-sql-driver/mysql"

"Time"

)

Func Main () {

Db,err: = sql. Open ("MySQL", "Astaxie:astaxie@/test?charset=utf8")

Checkerr (ERR)

                  // Inserting Data

Stmt,err: = db. Prepare ("INSERT userinfo setusername=?,departname=?,created=?")

Checkerr (ERR)

Res,err: = stmt. Exec ("Astaxie", " Research and Development department ", "2012-12-09")

Checkerr (ERR)

Id,err: = Res. Lastinsertid ()

Checkerr (ERR)

Fmt. PRINTLN (ID)

                  // Update Data

Stmt,err = db. Prepare ("Update userinfo set username=?") where uid=? ")

Checkerr (ERR)

Res,err = stmt. Exec ("astaxieupdate", id)

Checkerr (ERR)

Affect,err: = Res. Rowsaffected ()

Checkerr (ERR)

Fmt. PRINTLN (Affect)

                  // Querying Data

Rows,err: = db. Query ("SELECT * from UserInfo")

Checkerr (ERR)

Forrows. Next () {

Varuid int

Varusername string

Vardepartment string

varcreated string

err= rows. Scan (&uid, &username, &department, &created)

Checkerr (ERR)

Fmt. PRINTLN (UID)

Fmt. PRINTLN (username)

Fmt. PRINTLN (department)

Fmt. PRINTLN (created)

}

                  // Delete Data

Stmt,err = db. Prepare ("Delete from userinfo where uid=?")

Checkerr (ERR)

Res,err = stmt. Exec (ID)

Checkerr (ERR)

Affect,err = Res. Rowsaffected ()

Checkerr (ERR)

Fmt. PRINTLN (Affect)

Db. Close ()

}

Func Checkerr (err error) {

Iferr! = Nil {

Panic (ERR)

}

}

Sql. The open () function is used to turn on a registered database driver, the MySQL database driver is registered in Go-mysql-driver, and the second parameter is the DNS (Data Source Name). It is a database link and configuration information defined by Go-mysql-driver.

The following formats are supported:

User@unix (/path/to/socket)/dbname?charset=utf8user:password@tcp (localhost:5555)/dbname?charset=utf8

User:password@/dbname

USER:PASSWORD@TCP ([de:ad:be:ef::ca:fe]:80)/dbname

Db. The Prepare () function is used to return the SQL operation that is ready to be executed and then return to the ready execution state.

Db. The Query () function is used to directly execute SQL to return the rows result.

stmt. The Exec () function is used to execute stmt prepared SQL statements. We can see that the parameters we pass in are all =? corresponding data, so that the way to do this can prevent SQL injection to some extent.

Execution results

1

1

1

Astaxieupdate

Research and Development Department

2012-12-09

1

Related Article

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.