Markh
Markh

Reputation: 11

Go/golang sqlite query not returning any rows

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.

Upvotes: 0

Views: 3897

Answers (2)

Markh
Markh

Reputation: 11

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.

Upvotes: 0

David Budworth
David Budworth

Reputation: 11646

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)

Upvotes: 2

Related Questions