Go/Golang的SQLite查询没有返回任何行

huangapple go评论93阅读模式
英文:

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(&quot;index: %d - randomnr: %d  \n&quot;, id, RandomNr)




	
rows, errROW := db.Query(&quot;SELECT user.id,user.name,stage.link,player.url,player.characterchance,player.chance FROM user,stage,player WHERE user.id = &#39;%d&#39; AND &#39;%d&#39; &lt;= user.chance AND stage.user = user.id AND stage.domain = player.id AND player.chance &gt; 0 ORDER BY player.id ASC \n&quot;,id, RandomNr)//.Scan(&amp;idr, &amp;name, &amp;link, &amp;url, &amp;characterchance, &amp;chance)



//this is what the finished query looks like and it returns the rows as its supposed to	
//rows, errROW := db.Query(&quot;SELECT user.id,user.name,stage.link,player.url,player.characterchance,player.chance FROM user,stage,player WHERE user.id = &#39;203&#39; AND &#39;33&#39; &lt;= user.chance AND stage.user = user.id AND stage.domain = player.id AND player.chance &gt; 0 ORDER BY player.id ASC&quot;)

if errROW != nil {
	fmt.Println(&quot;errROW&quot;)
	log.Println(errROW)
	
}
defer rows.Close()
if rows.Next() == false {
	
	fmt.Println(&quot;no rows &quot;)
	
}
for rows.Next() {
	fmt.Println(&quot;where are the rows&quot;)
	var id int
	var name string
	var link string
	var url string
	var characterchance int
	var chance int
	
	rows.Scan(&amp;id, &amp;name, &amp;link, &amp;url, &amp;characterchance, &amp;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.

huangapple
  • 本文由 发表于 2015年5月9日 20:43:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/30140078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定