go-sqlite icon indicating copy to clipboard operation
go-sqlite copied to clipboard

Possible parsing bug with semicolons

Open tgf9 opened this issue 2 years ago • 0 comments

If a query ends in a semicolon and there's another character after the semicolon, db.Query returns the following error.

bad parameter or other API misuse: not an error (21)

Here's a Go program that reproduces the issue.

package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/glebarez/go-sqlite"
)

func main() {
	db, err := sql.Open("sqlite", ":memory:")
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()

	if _, err := db.Exec("create table foo(id integer primary key, bar text)"); err != nil {
		log.Println(err)
		return
	}

	if _, err := db.Exec("insert into foo (bar) values (?)", "bar"); err != nil {
		log.Println(err)
		return
	}

	qs := []string{
		// bad parameter or other API misuse: not an error (21)
		"select id from foo limit 1; ",
		"select id from foo limit 1;\n",
		"select id from foo limit 1;\t",
		" select id from foo limit 1; ",

		// OK
		"select id from foo limit 1;",
		" select bar from foo limit 1;",
		"select bar from foo limit 1 ",
		" select bar from foo limit 1",
	}

	for _, q := range qs {
		var bar string
		if err := db.QueryRow(q).Scan(&bar); err != nil {
			fmt.Printf("FAIL: %q\n%s\n\n", q, err)
			continue
		}
		fmt.Printf("OK: %q\n", q)
	}
}

For github.com/glebarez/go-sqlite v1.21.2

Another SQLite library github.com/mattn/go-sqlite3 v1.14.17 does not exhibit this behavior.

tgf9 avatar Oct 04 '23 00:10 tgf9