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

attach database issue

Open guybrand opened this issue 10 years ago • 1 comments

We have used : ATTACH DATABASE 'blabla.db' As 'blabla'; after which PRAGMA database_list; returns 'blabla' on the list , also when running a select or update on a table from 'blabla.blatbl' it works fine .

every now and a while we are getting an error of "no such table: blatbl" at this point the PRAGMA database_list; returns only the main db .

we use the Open method only once on load (and already checked if we ever open the DB again, and approved db only opens once on load) , we can only suspect that when in need of concurrent statement run , perhaps another connection to the db is opened without the attached db .

any ideas ?

guybrand avatar Jul 07 '16 16:07 guybrand

I had the same issue attaching a database and setting pragmas. You are correct about the cause. Go is creating a new connection to the database without your attached database. The way to ensure the attach happens for all connections is to use the connection hook for the driver.

sql.Register("sqlite3_hooked",
        &sqlite3.SQLiteDriver{
            ConnectHook: func(conn *sqlite3.SQLiteConn) error {
                conn.Exec("ATTACH DATABASE 'blabla.db' AS 'blalba';", nil)
                return nil
            },
        })

db, err := sql.Open("sqlite3_hooked", "main.db")

blackheart01 avatar Sep 15 '16 18:09 blackheart01