Go MySQL first glimpse. Inquire

Source: Internet
Author: User
Tags install go

First a few lines of grass mud horse grass mud horse ...

First of all to install Go-sql-driver/mysql, before downloading the installation to ensure that your gopath corresponds to your project directory

Export Gopath=/var/www/gogogo/test

Then download the installation

Go Install Github.com/go-sql-driver/mysql

Go get Github.com/go-sql-driver/mysql

In Main.go

Add _ "Github.com/go-sql-driver/mysql" and "Database/sql" in Import

Then a linked database, MSYQL.

DB, err: = SQL. Open ("MySQL", "User name: Password @/database? Charset=utf8")

First to inquire

There are three fields in my database (ID,NAME,PWD)

So I've defined a struct type.

Type User struct{

Id string

Name string

PWD string

}

Define a struct (definition?) Statement? It's a little confusing, anyway.

var u User

Stmtout, err: = db.  Prepare ("Select * from user where id=?") Registering an SQL statement

Watch out! Watch out! I've been stuck here for a long time ~ ~ Grass Mud Horse

Err = Stmtout.queryrow (5). Scan (&u.id,&u.name,&u.pwd)

  First of all, the first 5 is the value I upload to SQL, the placeholder? of this.

And then in my SQL, the data I checked out was "3 fields."

  

This means that it should be three, you have to write two. You idiot!

I originally defined the structure of only two attributes, and later use is also entered into two attributes. But the data is three properties, I add an ID in the struct and then scan () to add an ID

That's OK.

And then you get this err.

Nil when there's no data.

There is data that can be seen in the structure of this definition of U.

Finally, attach the code

      

Good night, ~~gogogo.

    

Go MySQL first glimpse. Inquire

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.