gpt4 book ai didi

mysql - 处理超过 20 条记录时出现问题

转载 作者:行者123 更新时间:2023-12-01 22:37:23 25 4
gpt4 key购买 nike

我正在使用 github.com/go-sql-driver/mysql与我的 MySQL 数据库交互的库。一切正常,除非我的查询超过 20 条记录。查询返回结果集很好,但是当我到达第 21 条记录时,当循环遍历它时会出现 panic 。

我以这种方式查询我的数据库:

row, err = store.db.Query(myquerystring)

我正在提取这样的返回值:
indx = 0
for row.Next() {
err = row.Scan(
&mySlice[indx].Val1, &mySlice[indx].Val2
)

if err != nil {
if err == sql.ErrNoRows {
log.Print("No records found")
} else {
log.Printf("Error retrieving data: %s", err.Error())
}
}
indx++
}

我得到的错误不是来自我的错误处理,它在检索第 21 条记录时立即发生。我将登录作为 for 循环中的第一件事和最后一件事,我得到了第 20 条记录末尾的日志被写入,但不是第 21 条记录的第一个日志,所以看起来 for row.Next()导致 panic 的行。

我得到的错误是:
http: panic serving xxx.xxx.xxx.xxx:xxxxx: runtime error: invalid memory address or nil pointer dereference

这就是我初始化 slice 的方式:

**sqlstring 与上面的查询相同,除了所有字段都从选择中删除并且只有 count(tableid)被退回。
row, err := store.db.Query(sqlString)

row.Next()
err = row.Scan(
&count,
)

var mySlice = make([]mystruct, count)

堆栈跟踪:
web_1  | 2019/11/21 03:03:23 4) record number: 19
web_1 | 2019/11/21 03:03:23 http: panic serving xxx.xxx.xxx.xxx:xxxxx: runtime error: invalid memory address or nil pointer dereference
web_1 | goroutine 811 [running]:
web_1 | net/http.(*conn).serve.func1(0xc42047eaa0)
web_1 | /usr/local/go/src/net/http/server.go:1721 +0xd0
web_1 | panic(0x7f8bc0, 0xa5c3a0)
web_1 | /usr/local/go/src/runtime/panic.go:489 +0x2cf
web_1 | database/sql.(*Rows).Next(0x0, 0x87afa9)
web_1 | /usr/local/go/src/database/sql/sql.go:2133 +0x30
web_1 | main.(*dbStore).GetVehicleCount(0xc420076230, 0x8865a0)
web_1 | /go/src/app/store.go:1385 +0xa7
web_1 | main.(*dbStore).GetVehicles(0xc420076230, 0x0, 0x0, 0x0)
web_1 | /go/src/app/store.go:1415 +0xed
web_1 | main.(*dbStore).SearchTrips(0xc420076230, 0xed5540400, 0x0, 0x0, 0x0, 0x0, 0x0)
web_1 | /go/src/app/store.go:1220 +0x123
web_1 | main.CalendarReportHandler(0xa373e0, 0xc4200e3420, 0xc4200ce500)
web_1 | /go/src/app/handlers.go:1559 +0x475
web_1 | net/http.HandlerFunc.ServeHTTP(0x886ca0, 0xa373e0, 0xc4200e3420, 0xc4200ce500)
web_1 | /usr/local/go/src/net/http/server.go:1942 +0x44
web_1 | app/vendor/github.com/gorilla/mux.(*Router).ServeHTTP(0xc4200d4180, 0xa373e0, 0xc4200e3420, 0xc4200ce500)
web_1 | /go/src/app/vendor/github.com/gorilla/mux/mux.go:212 +0xdf
web_1 | net/http.serverHandler.ServeHTTP(0xc42009bd90, 0xa373e0, 0xc4200e3420, 0xc4200ce300)
web_1 | /usr/local/go/src/net/http/server.go:2568 +0x92
web_1 | net/http.(*conn).serve(0xc42047eaa0, 0xa37ae0, 0xc4203a9600)
web_1 | /usr/local/go/src/net/http/server.go:1825 +0x612
web_1 | created by net/http.(*Server).Serve
web_1 | /usr/local/go/src/net/http/server.go:2668 +0x2ce
web_1 | 2019/11/21 03:03:23 Error retrieving drivers: Error 1040: Too many connections
web_1 | 2019/11/21 03:03:23 http: panic serving 172.26.0.1:58770: runtime error: invalid memory address or nil pointer dereference
web_1 | goroutine 955 [running]:
web_1 | net/http.(*conn).serve.func1(0xc4204e9040)
web_1 | /usr/local/go/src/net/http/server.go:1721 +0xd0
web_1 | panic(0x7f8bc0, 0xa5c3a0)
web_1 | /usr/local/go/src/runtime/panic.go:489 +0x2cf
web_1 | database/sql.(*Rows).Next(0x0, 0x87afa9)
web_1 | /usr/local/go/src/database/sql/sql.go:2133 +0x30
web_1 | main.(*dbStore).GetVehicleCount(0xc420076230, 0x0)
web_1 | /go/src/app/store.go:1385 +0xa7
web_1 | main.(*dbStore).SearchTrips(0xc420076230, 0xed54d6c80, 0x0, 0x0, 0x0, 0x0, 0x0)
web_1 | /go/src/app/store.go:1219 +0xf3
web_1 | main.CalendarReportHandler(0xa373e0, 0xc4200e3960, 0xc4200ce700)
web_1 | /go/src/app/handlers.go:1559 +0x475
web_1 | net/http.HandlerFunc.ServeHTTP(0x886ca0, 0xa373e0, 0xc4200e3960, 0xc4200ce700)
web_1 | /usr/local/go/src/net/http/server.go:1942 +0x44
web_1 | app/vendor/github.com/gorilla/mux.(*Router).ServeHTTP(0xc4200d4180, 0xa373e0, 0xc4200e3960, 0xc4200ce700)
web_1 | /go/src/app/vendor/github.com/gorilla/mux/mux.go:212 +0xdf
web_1 | net/http.serverHandler.ServeHTTP(0xc42009bd90, 0xa373e0, 0xc4200e3960, 0xc420144000)
web_1 | /usr/local/go/src/net/http/server.go:2568 +0x92
web_1 | net/http.(*conn).serve(0xc4204e9040, 0xa37ae0, 0xc4204e1940)
web_1 | /usr/local/go/src/net/http/server.go:1825 +0x612
web_1 | created by net/http.(*Server).Serve
web_1 | /usr/local/go/src/net/http/server.go:2668 +0x2ce

最佳答案

首先感谢您的堆栈跟踪请求,它让我用细齿梳分析堆栈跟踪,引导我找到解决方案。

有2个问题。

我填充 slice 的函数有 2 个其他函数被调用来填充我的父结构中的结构,由于父结构中返回的记录数量超过了填充父结构之前可用的最大连接数。在这些函数中显式关闭行对象可解决此错误。

第二个是我有一个 for 循环,我从 1 循环到记录计数,因为我想将值 1 存储到“X”而不是 0 到“X-1”,但这显然导致索引超出范围错误(菜鸟错误)。这很容易通过简单地正常循环并将“X+1”存储在变量中来解决。

感谢他们帮助我找到自己的问题的所有评论,非常感谢。

关于mysql - 处理超过 20 条记录时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58966325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com