Skip to content

Possible parsing bug with semicolons #119

@tgf9

Description

@tgf9

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions