英文:
go build error "rows.Columns undefined (type *sql.Row has no field or method Columns)"
问题
我想使用Golang从我的PostgreSQL数据库中打印多行多列的数据。在构建以下代码时,我遇到了一些错误:
package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
"log"
"runtime"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
db, err := sql.Open("postgres", "dbname=sample_data user=postgres password=postgres sslmode=disable")
defer db.Close()
if err != nil {
fmt.Println("error connecting to DB")
}
r := gin.Default()
r.GET("/cin_display", func(c *gin.Context) {
rows := db.QueryRow("SELECT cin FROM companies limit 1;")
columns, _ := rows.Columns()
count := len(columns)
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
for rows.Next() {
for i, _ := range columns {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
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)
}
}
})
}
func Connect(connectionString string) *sql.DB {
db, err := sql.Open("postgres", connectionString)
if err != nil {
log.Fatal(err)
}
return db
}
我得到了以下错误:
rows.Columns
未定义(*sql.Row 类型没有 Columns 字段或方法)rows.Next
未定义(*sql.Row 类型没有 Next 字段或方法)
如何解决这个问题?
英文:
I want to print multiple rows having multiple columns from my postgresql database using golang.while buildng the following code
package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
"log"
"runtime"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
db, err := sql.Open("postgres", "dbname=sample_data user=postgres password=postgres sslmode=disable")
defer db.Close()
if err != nil {
fmt.Println("error connecting to DB")
}
r := gin.Default()
r.GET("/cin_display", func(c *gin.Context) {
rows := db.QueryRow("SELECT cin FROM companies limit 1;")
columns, _ := rows.Columns()
count := len(columns)
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
for rows.Next() {
for i, _ := range columns {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
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)
}
}
})
}
func Connect(connectionString string) *sql.DB {
db, err := sql.Open("postgres", connectionString)
if err != nil {
log.Fatal(err)
}
return db
}
I am getting the errors like
> rows.Columns undefined (type *sql.Row has no field or method Columns)
>
> rows.Next undefined (type *sql.Row has no field or method Next)
how to solve this?
答案1
得分: 2
你需要的是 Query
,它会返回一个 *sql.Rows
,该类型具有你想要使用的方法。
英文:
QueryRow
returns a single *sql.Row
What you want is Query
, which will give you *sql.Rows
which has the methods you are trying to use.
答案2
得分: 1
你在这里屏蔽了database/sql包:sql := `select * from table
sql现在是一个字符串,而不是包。将该字符串重命名为query,它就会起作用。
英文:
you masked the database/sql package here: sql := `select * from table
sql is then a string, not the package. Rename the string e.g. to query and it will work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论