该Rows.Scan方法采用与 SQL 查询中的列数一样多的参数。
由于正在执行的查询是SHOW COLUMNS FROM my_table我不能省略任何我不需要的列(或者我可以吗?)。
有没有办法忽略查询结果集中不需要的某些字段?
下面是我的代码:
rows, err := db.Query("SHOW COLUMNS FROM " + r.Name)
DieIf(err)
//var field, dataType, ignoreMe1, ignoreMe2, ignoreMe3 string
var field, dataType string
for rows.Next() {
//This Place
// |
// V
if err := rows.Scan(&field, &dataType); err != nil {
DieIf(err)
}
r.Attributes[field] = Attribute{
Name: field,
DataType: dataType,
Constraint: false,
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
sql: expected 5 destination arguments in Scan, not 2
小智 4
因此,我在这里为您提供了一种解决方案,请尝试使用此解决方案来获取field查询type。
package main
import (
"fmt"
_ "github.com/lib/pq"
"database/sql"
)
func main() {
db, _ := sql.Open(
"postgres",
"user=postgres dbname=demo password=123456")
rows, _ := db.Query("SELECT * FROM tableName;")
columns, _ := rows.Columns()
count := len(columns)
values := make([]interface{}, count)
valuePtr := make([]interface{}, count)
for rows.Next() {
for i, _ := range columns {
valuePtr[i] = &values[i]
}
rows.Scan(valuePtr...)
for i, col := range columns {
var v interface{}
val := values[i]
b, ok := val.([]byte)
if (ok) {
v = string(b)
} else {
v = val
}
fmt.Println(col, v)
}
}
}
Run Code Online (Sandbox Code Playgroud)