获取类似于Python风格的行数据

huangapple go评论89阅读模式
英文:

Fetch rows like python-style

问题

在Python中,可以使用简单的db.query("SELECT id,login,password FROM Users")查询并返回列表[(1,'root','password'), (2,'toor','password')]。我可以简单地遍历它:

for user in response:
    print("id: %s, login: %s, password: %s" %(user[0],user[1],user[2]))

但是在Golang中,我找不到类似的简单方法来做到这一点。

我理解Python具有动态类型,而Golang具有静态类型。所以我正在寻找答案,也许有一些库提供这样的功能?或者有什么技巧可以实现?谢谢回答!

英文:

In python it's a simple db.query("SELECT id,login,password FROM Users") and return list [(1,'root','password'), (2,'toor','password')]. I can simple iterate it

for user in response:print("id: %s, login: %s, password: %s", %(user[0],user[1],user[2]))

But in Golang I can't find revelant example for simple way to do this.

I understand that a python has dynamic typing, golang is static. So I'm looking for an answer, maybe some libraries provide such functionality? Hacks? Thanks for answers!

答案1

得分: 1

你需要类似这样的代码,但是如果你使用了复杂的MySQL数据类型,比如enum、set等,可能会出现问题。

var (
    result    [][]string
    container []string
    pointers  []interface{}
)

rows, err := db.Query("SELECT id, login, password FROM Users")

if err != nil {
    panic(err.Error())
}

cols, err := rows.Columns()

if err != nil {
    panic(err.Error())
}

length := len(cols)

for rows.Next() {
    pointers = make([]interface{}, length)
    container = make([]string, length)

    for i := range pointers {
        pointers[i] = &container[i]
    }

    err = rows.Scan(pointers...)

    if err != nil {
        panic(err.Error())
    }

    result = append(result, container)
}

// 然后你可以遍历结果并使用fmt包进行打印

以上是翻译好的代码部分。

英文:

You need something like this, but there may be a problem if you are using complex mysql's data types like enum, set and etc.

var (
	result    [][]string
	container []string
	pointers  []interface{}
)

rows, err := db.Query("SELECT id, login, password FROM Users")

if err != nil {
	panic(err.Error())
}

cols, err := rows.Columns()

if err != nil {
	panic(err.Error())
}

length := len(cols)

for rows.Next() {
	pointers = make([]interface{}, length)
	container = make([]string, length)

	for i := range pointers {
		pointers[i] = &container[i]
	}

	err = rows.Scan(pointers...)

	if err != nil {
		panic(err.Error())
	}

	result = append(result, container)
}

Then you can loop over results and print it using fmt package.

答案2

得分: 0

一个很棒的库,可以很好地扩展Go标准库的SQL接口是sqlx

使用这个库,你可以像在Python中的例子一样做一些类似的事情:

db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
if err != nil {    
    log.Fatalln(err)
}
users := []User{}
db.Select(&users, "SELECT id,login,password FROM Users")
//遍历用户切片
英文:

An awesome library that nicely extends the go standard lib sql interface is sqlx

Using this lib, you can do something similar to your example in python:

db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
if err != nil {    
    log.Fatalln(err)
}
users := []User{}
db.Select(&users, "SELECT id,login,password FROM Users")
//iterate through the users slice

huangapple
  • 本文由 发表于 2017年4月29日 01:30:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/43685787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定