💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
> ### 第七例 sqlx操作 ~~~ package demo4 //doc : https://github.com/jmoiron/sqlx import ( "github.com/jmoiron/sqlx" "fmt" _ "github.com/go-sql-driver/mysql" "runtime" ) type Article struct { Id int `db:"id"` Title string `db:"title"` Content string `db:"content"` ViewNum int `db:"viewnum"` Time int `db:"time2"` } func Demo4() { host := "localhost" dbname := "messagebox" username := "root" password := "123456" db, err := sqlx.Open("mysql", username+":"+password+"@tcp("+host+")/"+dbname+"?charset=utf8") SimplePanic(err) defer db.Close() article := Article{} err = db.Get(&article, "select id, title, content, viewnum, time as time2 from msg_article order by time desc limit 1") SimplePanic(err) fmt.Println(article) articles := make([]Article, 0) err = db.Select(&articles, "select id, title, content, viewnum, time as time2 from msg_article limit 10") SimplePanic(err) fmt.Println(articles) } func SimplePanic(err error) { if err != nil { _, file, line, _ := runtime.Caller(1) fmt.Println(file, line, err) runtime.Goexit() } } ~~~