我刚开始尝试从 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)//.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()
}
}
此查询可以返回多行和单行。我还尝试通过 QueryRow 将数据检索为单行,但也没有返回任何结果。
如有任何帮助,我们将不胜感激。
更新: 我加了 如果 rows.Next() == false
试图找出问题所在。删除它会产生相同的结果。此外,我没有从扫描中收到错误消息。 for rows.next() 循环甚至不会执行。
最佳答案
当你这样做时:
if rows.Next() == false
你正在滚动到第一行
和
for rows.Next()
移动到下一行
基本上,您将跳过您提供的示例代码中结果集中的第一行。
此外,您忽略了扫描中的错误。
如果查询返回至少 2 行(因为第一行被跳过),这看起来会打印一些东西
关于sqlite - Go/golang sqlite 查询不返回任何行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30140078/