package main
import (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
func main() {
/*
//打开数据库
db, err := sql.Open("mysql", "root:123@/tsong")
if err != nil {
panic("open database fail")
}
//链接数据库
err = db.Ping()
if err != nil {
panic("connect database fail")
}
//链接数据库成功,就可以正确的操作数据库了,执行数据库语句
data, err := db.Query("select * from tsong where count < 10", data)
var str interface{}
str1 := "hello"
str = str1
a := str.(int)
fmt.Println(a, str)
type NotKnowType struct {
s1, s2, s3 string
}
var secret interface{} = NotKnowType{"a", "b", "c"}
value := reflect.ValueOf(secret)
el := value.Elem()
fmt.Println(el)
for i := 0; i < value.NumField(); i++ {
fmt.Println(value.Field(i))
}
var c io.Reader
fmt.Println(reflect.TypeOf(c))
c = os.Stdout
fmt.Println(reflect.TypeOf(c))
c = bufio.NewReader(c)
fmt.Println(reflect.TypeOf(c))
boring("boring")
time.Durati
func boring(msg string) {
for i := 0; ; i++ {
fmt.Println(msg, i)
time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond)
}
*/
db, err := sql.Open("mysql", "root:1@tcp(localhost:3306)/student")
if err != nil {
panic("open database fail")
}
err = db.Ping()
if err != nil {
panic("linked database fail")
}
data, err := db.Query("select *from tb_student")
if err != nil {
panic("operation database fail")
}
//println(data)
for data.Next() {
var name string
var id int
var phoneNumber string
data.Scan(&name, &id, &phoneNumber)
println(name, id, phoneNumber)
}
}
go数据库操作
最新推荐文章于 2024-07-16 15:04:21 发布