1
twm 2017-03-02 19:19:35 +08:00 via iPhone 1
每个语言都有游戏规则 遵从或放弃
|
2
loading 2017-03-02 19:31:48 +08:00 via Android
希望能帮到你:
stmt, err := db.Prepare(qStr) if err != nil { return err } defer stmt.Close() rows, err := stmt.Query() if err != nil { return err } defer rows.Close() columns, err := rows.Columns() if err != nil { return err } count := len(columns) tableData := make([]map[string]interface{}, 0) values := make([]interface{}, count) valuePtrs := make([]interface{}, count) for rows.Next() { for i := 0; i < count; i++ { valuePtrs[i] = &values[i] } rows.Scan(valuePtrs...) entry := make(map[string]interface{}) for i, col := range columns { var v interface{} val := values[i] b, ok := val.([]byte) if ok { v = string(b) } else { v = val } entry[col] = v } tableData = append(tableData, entry) |
3
loading 2017-03-02 19:32:11 +08:00 via Android
卧槽
|
5
scnace 2017-03-02 19:45:57 +08:00 via Android
可以考虑下 ORM🐸
|
6
loading 2017-03-02 19:50:31 +08:00 1
|
7
Muninn 2017-03-02 20:35:05 +08:00 via Android
静态强类型语言这方面完全没法和动态弱类型比
业务逻辑不复杂只把数据库当存储的话 考虑下 gorm |
8
ikaros 2017-03-02 20:58:30 +08:00
|
9
ykrl089 2017-03-02 21:06:20 +08:00
你需要一个 orm
|
10
Zzzzzzzzz 2017-03-02 21:14:11 +08:00
sqlx
|
11
fuxiaohei 2017-03-02 21:29:54 +08:00
sqlx + 1
|
13
buseni OP @loading 就是今天学习,写了一个数据库操作,感觉这样 scan 用着不爽,吐槽下,一会看看你的代码,多谢指导
|
14
jarlyyn 2017-03-02 22:15:16 +08:00
……
type User struct{ ID int username string salt string password string } ... user=User{} stmt,err:=tx.prepare(`select id ,username,salt,password from ....`) err!=row.scan(&user.id,&userd.username,&username.salt,&user.password) if err!=nil{ return err } err:=user.auth() 大概这样吧,我觉得还行啊。 用反射的话, join 查询是不是会很蛋疼? 再不行就上 orm 呗 |
15
jarlyyn 2017-03-02 22:17:22 +08:00
这格式……
总之我是 stmt,err:=tx.prepare(`select id , username, salt, password from ....`) err!=row.scan( &user.id, &userd.username, &username.salt, &user.password) 一个对一个,感觉还行 |
17
mingyun 2017-03-02 22:59:48 +08:00
毕竟 php 是最好的语言
|
18
jarlyyn 2017-03-03 01:18:33 +08:00
@loading
var params, countParams []interface{} querySelect := `select node.id,node.title,node.created_time,node.updated_time,node.url,node.picture,node.category,node.nodetype,node.keyword,node.content, nodecategory.id,nodecategory.keyword,nodecategory.title,nodecategory.nodetype,nodecategory.comment FROM node,nodecategory ` queryWhere := "where node.category=nodecategory.id " if catalog > -1 { queryWhere = queryWhere + " AND node.category= ? " params = append(params, catalog) countParams = params } queryOther := " ORDER by node.created_time DESC LIMIT ? OFFSET ?" offset := pagesize * (page - 1) params = append(params, pagesize, offset) stmt, err := tx.Prepare(querySelect + queryWhere + queryOther) if err != nil { return nil, 0, err } defer stmt.Close() var rows *sql.Rows rows, err = stmt.Query(params...) if err != nil { return nil, 0, err } defer rows.Close() for rows.Next() { nm := NodeModel{} nm.CategoryModel = CategoryModel{} err = rows.Scan( &nm.Id, &nm.Title, &nm.CreatedTime, &nm.UpdatedTime, &nm.Url, &nm.Picture, &nm.Category, &nm.Nodetype, &nm.Keyword, &nm.Content, &nm.CategoryModel.Id, &nm.CategoryModel.Keyword, &nm.CategoryModel.Title, &nm.CategoryModel.Nodetype, &nm.CategoryModel.Comment) if err != nil { return nil, 0, err } result = append(result, nm) } 这样? |
19
jarlyyn 2017-03-03 01:22:21 +08:00
|