mattn / go-sqlite3

sqlite3 driver for go using database/sql

Home Page:http://mattn.github.io/go-sqlite3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changing PRAGMA user_version via placeholders does not work; Without placeholders it works fine

flokoe opened this issue · comments

Hi there,

I want to change PRAGMA user_version, but if I use placeholders in my Exec string, it does not work. Instead I get the following error:

near "?": syntax error

Placeholders in other Exec statements and changing user_version directly within the query without using placeholders works just fine. I could not find any info about this. Is this intended behavior?

I am a GO beginner, so if this a me problem, feel free to close this issue.

Here is some example code to reproduce this issue:

package main

import (
	"database/sql"
	"log"

	_ "github.com/mattn/go-sqlite3"
)

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

	var version int

	// works just fine
	_, err = db.Exec("PRAGMA user_version = 42")
	if err != nil {
		log.Fatal(err)
	}

	err = db.QueryRow("PRAGMA user_version").Scan(&version)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Current user version:", version)

	// does not work
	_, err = db.Exec("PRAGMA user_version = ?", 1337)
	if err != nil {
		log.Fatal(err)
	}

	err = db.QueryRow("PRAGMA user_version").Scan(&version)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Current user version:", version)
}

Best regards
Florian

I don't think SQLite supports parameter bindings in PRAGMAs.

I don't think SQLite supports parameter bindings in PRAGMAs.

Ah, you are right. I did not even think about that this was a constraint of SQLite itself. Thanks for your help anyway!