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