我是新来的,试图从sqlite数据库检索数据。 我使用github.com/mattn/go-sqlite3作为sqlite驱动程序。Go/golang 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()==假
是试图发现问题。删除它会得到相同的结果。此外,我不会收到来自扫描的错误消息。 for rows.next()循环甚至不会执行。
没有想法的人? – Markh