MySQL operation of Golang

Source: Internet
Author: User

said that year fornicated to the computer a crackling,, to MySQL add and delete changes to increase the insert
Package Mainimport ("FMT"    "GITHUB.COM/JMOIRON/SQLX"    _ "Github.com/go-sql-driver/mysql" //To initialize a MySQL driver, you must) Type personstruct{UserIdint' DB:"user_id"' Usernamestring' DB:"username"' Sexstring' DB:"Sex"' Emailstring' DB:"Email"'}type Placestruct{Countrystring' DB:"Country"' Citystring' DB:" City"' Telcodeint' DB:"Telcode"`}varDb *SQLX. Dbfunc init () {//"MySQL" Specifies the database type,/test the specified database root:123 colon-separated password root account 123 PasswordDatabase, err: = Sqlx. Open ("MySQL","[email protected] (127.0.0.1:3306)/test")    ifErr! =Nil {fmt. Println ("Open MySQL failed,", Err)return} Db=Database}func Main () {//inserting data into a tableR, Err: = Db.exec ("INSERT into person (username,sex,email) VALUES (?,?,?)","Chaoge","Mans","[email protected]")    ifErr! =Nil {fmt. Println ("exec failed,", Err)return} ID, err:=R.lastinsertid ()ifErr! =Nil {fmt. Println ("exec failed,", Err)return} fmt. Println ("Insert SUCC:", id)}
InsertQuery Select
Package Mainimport ("FMT"    _ "Github.com/go-sql-driver/mysql"    "GITHUB.COM/JMOIRON/SQLX") Type personstruct{UserIdint' DB:"user_id"'`Usernamestring' DB:"username"' Sexstring' DB:"Sex"' Emailstring' DB:"Email"'}type Placestruct{Countrystring' DB:"Country"' Citystring' DB:" City"' Telcodeint' DB:"Telcode"`}varDb *SQLX. Dbfunc init () {//initializing a linked databaseDatabase, err: = Sqlx. Open ("MySQL","Root: @tcp (127.0.0.1:3306)/test")    ifErr! =Nil {fmt. Println ("Open MySQL failed", Err)return} Db=Database}func Main () {varPerson []person err:= Db.select (&person,"Select User_id,username,sex,email from person where user_id=?",1)    ifErr! =Nil {fmt. Println ("exec failed,", Err)return} fmt. Println ("Select SUCC:", person)}
SelectChange update
Package Mainimport ("FMT"    _ "Github.com/go-sql-driver/mysql"    "GITHUB.COM/JMOIRON/SQLX") Type personstruct{UserIdint' DB:"user_id"' Usernamestring' DB:"username"' Sexstring' DB:"Sex"' Emailstring' DB:"Email"'}type Placestruct{Countrystring' DB:"Country"' Citystring' DB:" City"' Telcodeint' DB:"Telcode"`}varDb *SQLX. Dbfunc init () {database, err:= Sqlx. Open ("MySQL","[email protected] (127.0.0.1:3306)/test")    ifErr! =Nil {fmt. Println ("Open MySQL failed,", Err)return} Db=Database}func Main () {_, Err:= Db.exec ("Update person set username=? where user_id=?","chaoge666",1)    ifErr! =Nil {fmt. Println ("exec failed,", Err)}}
UpdateDelete Delete
Package Mainimport ("FMT"    _ "Github.com/go-sql-driver/mysql"    "GITHUB.COM/JMOIRON/SQLX") Type personstruct{UserIdint' DB:"user_id"' Usernamestring' DB:"username"' Sexstring' DB:"Sex"' Emailstring' DB:"Email"'}type Placestruct{Countrystring' DB:"Country"' Citystring' DB:" City"' Telcodeint' DB:"Telcode"`}varDb *SQLX. Dbfunc init () {//initializing a linked databaseDatabase, err: = Sqlx. Open ("MySQL","[email protected] (127.0.0.1:3306)/test")    ifErr! =Nil {fmt. Println ("Open MySQL failed,", Err)return} Db=Database}func Main () {_, Err:= Db.exec ("Delete from the person where user_id=?",1)    ifErr! =Nil {fmt. Println ("exec failed,", Err)return} fmt. Println ("Delete succ")}
Delete

MySQL operation of 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.