golang操作mysql数据库
go get github.com/go-sql-driver/mysql
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type Class struct {
id int
name string
}
go
func main() {
db, _ := sql.Open("mysql", "root:12345@/test1")
defer db.Close()
//result,err:=db.Exec("insert into student_class ( class_name) VALUES (?)","class1")
//fmt.Println(result)
//fmt.Println(err)
rows,err:=db.Query("select * from student_class")
fmt.Println(rows)
fmt.Println(err)
defer rows.Close()
class1:=&Class{}
for rows.Next() {
rows.Scan(&class1.id,&class1.name)
}
fmt.Println(class1)
}