我是新手,试图从 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 作为单行检索数据,但也没有返回任何结果。
任何帮助将非常感激。
更新:我添加了 if rows.Next() == false
作为寻找问题的尝试。删除它会产生相同的结果。此外,我没有从扫描中收到错误消息。for rows.next() 循环甚至不会被执行。
慕哥9229398
红糖糍粑
相关分类