Created
February 27, 2012 09:22
-
-
Save mikespook/1922748 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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