Skip to content

Instantly share code, notes, and snippets.

@mikespook
Created February 27, 2012 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikespook/1922748 to your computer and use it in GitHub Desktop.
Save mikespook/1922748 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"database/sql"
"encoding/json"
_ "github.com/ziutek/mymysql/godrv"
)
type Dict struct {
Code, Name string
}
func main() {
db, err := sql.Open("mymysql", "hms/root/xxiyy")
if err != nil {
log.Panic(err)
}
_, err = db.Query("SET NAMES utf8")
if err != nil {
log.Panic(err)
}
rows, err := db.Query("select * from dicts")
if err != nil {
log.Panic(err)
}
var result []Dict
for rows.Next() {
var dict Dict
err := rows.Scan(&dict.Code, &dict.Name)
if err != nil {
log.Panic(err)
}
result = append(result, dict)
}
j, err := json.Marshal(result)
log.Print(string(j))
err = db.Close()
if err != nil {
log.Panic(err)
}
}
CREATE TABLE IF NOT EXISTS `dicts` (
`code` char(32) NOT NULL,
`name` char(32) NOT NULL,
PRIMARY KEY (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `dicts` (`code`, `name`) VALUES
('BBT', '基础体温');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment