【发布时间】:2020-04-25 00:29:12
【问题描述】:
我正在编写一个通用代码来查询任何 RDS 表中的数据。我已经阅读了许多 StackOverflow 答案,但没有一个对我有用。我浏览了以下链接:-
- panic: sql: expected 1 destination arguments in Scan, not <number> golang, pq, sql
- How to query any table of RDS using Golang SDK
我的第一个代码是
package main
import (
"fmt"
)
type BA_Client struct {
ClientId int `json:ClientId;"`
CompanyName string `json:CompanyName;"`
CreateDate string `json:CreateDate;"`
}
func main() {
conn, _ := getConnection() // Det database connection
query := `select * from IMBookingApp.dbo.BA_Client
ORDER BY
ClientId ASC
OFFSET 56 ROWS
FETCH NEXT 10 ROWS ONLY ;`
var p []byte
err := conn.QueryRow(query).Scan(&p)
if err != nil {
fmt.Println("Error1", err)
}
fmt.Println("Data:", p)
var m BA_Client
err = json.Unmarshal(p, &m)
if err != nil {
fmt.Println("Error2", err)
}
}
我的第二个代码是
conn, _ := getConnection()
query := `select * from IMBookingApp.dbo.BA_Client__c
ORDER BY
ClientId__c ASC
OFFSET 56 ROWS
FETCH NEXT 10 ROWS ONLY ;`
rows, err := conn.Query(query)
if err != nil {
fmt.Println("Error:")
log.Fatal(err)
}
println("rows", rows)
defer rows.Close()
columns, err := rows.Columns()
fmt.Println("columns", columns)
if err != nil {
panic(err)
}
for rows.Next() {
receiver := make([]*string, len(columns))
err := rows.Scan(&receiver )
if err != nil {
fmt.Println("Error reading rows: " + err.Error())
}
fmt.Println("Data:", p)
fmt.Println("receiver", receiver)
}
使用这两个代码,我得到与以下相同的错误
sql: expected 3 destination arguments in Scan, not 1
可能是因为我使用的是 SQL Server 而不是 MySQL?感谢是否有人可以帮助我找到问题。
【问题讨论】:
标签: sql sql-server go