0

It seems golang's sqlite package doesn't like my db.Query statement, though it's exactly like the one found in the example on github.

db, err := sql.Open("sqlite3", "./database.db")
if err != nil {
    log.Fatal(err)
}
defer db.Close()

rows, err = db.Query("select id, name from job")
if err != nil {
    log.Fatal(err)
}   
defer rows.Close()

fmt.Println("Jobs:")
for rows.Next() {
    var name string
    var id int
    fmt.Printf("%v %v\n", id, name)
}  

This is the error I'm getting:

./test.go:7: undefined: rows
./test.go:7: cannot assign to rows
./test.go:11: undefined: rows
./test.go:14: undefined: rows

Edit: I've tried using grave accent and single quote strings for db.Query() as well, to no avail.

2
  • 1
    Look at using jmoiron.github.io/sqlx to simplify the need to call rows.Next. Commented Aug 20, 2014 at 21:57
  • upvoted; some people don't speak compiler error. Commented Aug 21, 2014 at 12:25

1 Answer 1

4

You cannot assign values to to undeclared variables.

rows, err = db.Query("select id, name from job")

Should be :

rows, err := db.Query("select id, name from job")

Theoretically this should solve the problem, but I haven't tried.

You should also add :

rows.Scan(&id, &name)

Before the printf function so as to actually assign the row's value to the id & name variables otherwise will print an empty string & 0.

Sign up to request clarification or add additional context in comments.

2 Comments

This did solve the problem. Thanks for answering, most people just downvote to oblivion. I combed through my code for about 20 minutes before asking for help. Sometimes those small syntax errors slip through the cracks.
@bvpx I answered the question because I knew how to fix your problem, but I also down voted because it was a low quality question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.