英文:
Go/golang sqlite query not returning any rows
问题
我是你的中文翻译助手,以下是翻译好的内容:
我刚开始学习Go语言,并尝试从SQLite数据库中检索数据。我正在使用github.com/mattn/go-sqlite3作为SQLite驱动程序。
尽管应该返回结果,但我发送的查询没有返回任何结果。我尝试手动执行程序生成的查询,它返回了正确的数据,当我通过程序发送查询时也是如此。
以下是我的代码:
for index := range Array {
id, _ := strconv.Atoi(Array[index])
rand.Seed(time.Now().UnixNano())
RandomNr := rand.Intn(100)
fmt.Printf("index: %d - randomnr: %d \n", id, RandomNr)
rows, errROW := db.Query("SELECT user.id,user.name,stage.link,player.url,player.characterchance,player.chance FROM user,stage,player WHERE user.id = '%d' AND '%d' <= user.chance AND stage.user = user.id AND stage.domain = player.id AND player.chance > 0 ORDER BY player.id ASC \n",id, RandomNr)
// 这是最终查询的样子,它返回了预期的行
// rows, errROW := db.Query("SELECT user.id,user.name,stage.link,player.url,player.characterchance,player.chance FROM user,stage,player WHERE user.id = '203' AND '33' <= user.chance AND stage.user = user.id AND stage.domain = player.id AND player.chance > 0 ORDER BY player.id ASC")
if errROW != nil {
fmt.Println("errROW")
log.Println(errROW)
}
defer rows.Close()
if rows.Next() == false {
fmt.Println("no rows")
}
for rows.Next() {
fmt.Println("where are the rows")
var id int
var name string
var link string
var url string
var characterchance int
var chance int
rows.Scan(&id, &name, &link, &url, &characterchance, &chance)
fmt.Println(id, name, link, url, characterchance, chance)
}
rows.Close()
}
这个查询可以返回多行和单行。我还尝试使用QueryRow作为单行检索数据,但也没有返回任何结果。
非常感谢任何帮助!
更新:
我添加了if rows.Next() == false
作为查找问题的尝试。删除它会得到相同的结果。此外,我没有从Scan
中得到错误消息。for rows.Next()
循环甚至没有执行。
英文:
I am new to go and trying to retrieve data from a sqlite database.
Im using github.com/mattn/go-sqlite3 as sqlite driver.
The query I m sending does not return any results even though it should. I tried the query my programme is generating manually and it returns data as it should when I use the query manually as well as when I send it via my programme.
Here is my code:
for index := range Array {
id, _ := strconv.Atoi(Array[index])
rand.Seed(time.Now().UnixNano())
RandomNr := rand.Intn(100)
fmt.Printf("index: %d - randomnr: %d \n", id, RandomNr)
rows, errROW := db.Query("SELECT user.id,user.name,stage.link,player.url,player.characterchance,player.chance FROM user,stage,player WHERE user.id = '%d' AND '%d' <= user.chance AND stage.user = user.id AND stage.domain = player.id AND player.chance > 0 ORDER BY player.id ASC \n",id, RandomNr)//.Scan(&idr, &name, &link, &url, &characterchance, &chance)
//this is what the finished query looks like and it returns the rows as its supposed to
//rows, errROW := db.Query("SELECT user.id,user.name,stage.link,player.url,player.characterchance,player.chance FROM user,stage,player WHERE user.id = '203' AND '33' <= user.chance AND stage.user = user.id AND stage.domain = player.id AND player.chance > 0 ORDER BY player.id ASC")
if errROW != nil {
fmt.Println("errROW")
log.Println(errROW)
}
defer rows.Close()
if rows.Next() == false {
fmt.Println("no rows ")
}
for rows.Next() {
fmt.Println("where are the rows")
var id int
var name string
var link string
var url string
var characterchance int
var chance int
rows.Scan(&id, &name, &link, &url, &characterchance, &chance)
fmt.Println(id,name,link,url,characterchance,chance)
}
rows.Close()
}
}
This query can return multiple and single rows. I also tried retrieving the data via QueryRow as a single row which also did not return any result.
Any help would be much appreciated.
UPDATE:
I added
if rows.Next() == false
as an attempt to find the problem. Removing it yields the same result. Furthermore I do not get an error message from scan. The for rows.next() loop does not even get executed.
答案1
得分: 2
当你执行以下代码时:
if rows.Next() == false
你正在滚动到第一行。
而且
for rows.Next()
会移动到下一行。
基本上,在你提供的示例代码中,你跳过了结果集中的第一行。
此外,你忽略了Scan中的错误。
这看起来像是在查询返回至少2行时会打印一些内容(因为第一行被跳过了)。
英文:
when you do:
if rows.Next() == false
you are scrolling to the first row
and
for rows.Next()
moves to the next row
basically, you are skipping the first row in your result set in the example code you provided.
also, you are ignoring the error in Scan.
This looks like it would print something if the query returns at least 2 rows (since first row is being skipped)
答案2
得分: 0
好的,以下是翻译好的内容:
好的,我找到了问题所在:
在我的查询中,我使用了%d作为变量的占位符,但实际上应该使用$1、$2等。使用这种方式,查询将按预期返回结果。
对我来说,这种行为很奇怪,它不会引发任何来自Go或SQLite的错误,甚至在你只是打印出查询并手动在sqlite3中使用时也能正常工作。对于刚开始使用Go的C程序员来说,这显然可能是一些头痛的原因。
英文:
Ok I figured out what the problem was:
In my query I used: %d as a placeholder for my variable when I should have used $1,$2 etc. Using this the query returns results as expected.
It seems strange to me that this behaviour is allowed returns no error from go or sqlite and even works when you just printout the query and use it with sqlite3 manually. Coming from C and just starting out with go this can obviously be the cause for some headaches.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论