By loading extensions this way, it's not possible to later load extensions using db.Exec, which improves security, and makes it much easier to load extensions correctly. The zero value for the slice (the empty slice) loads no extensions by default. The extension example has been updated to use this much simpler system. The ConnectHook field is still in SQLiteDriver in case it's needed for other driver-wide initialization. Updates #71 of mattn/go-sqlite3.
44 lines
802 B
Go
44 lines
802 B
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"github.com/mattn/go-sqlite3"
|
|
"log"
|
|
)
|
|
|
|
func main() {
|
|
sql.Register("sqlite3_with_extensions",
|
|
&sqlite3.SQLiteDriver{
|
|
Extensions: []string{
|
|
"sqlite3_mod_regexp.dll",
|
|
},
|
|
})
|
|
|
|
db, err := sql.Open("sqlite3_with_extensions", ":memory:")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
// Force db to make a new connection in pool
|
|
// by putting the original in a transaction
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer tx.Commit()
|
|
|
|
// New connection works (hopefully!)
|
|
rows, err := db.Query("select 'hello world' where 'hello world' regexp '^hello.*d$'")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var helloworld string
|
|
rows.Scan(&helloworld)
|
|
fmt.Println(helloworld)
|
|
}
|
|
}
|