Closed chivincent closed 1 day ago
Please note the following:
*sql.DB
represents a connection pool.Query
/QueryRow
/Exec
/etc. on *sql.DB
may run on a different connection in the pool.db.Conn
, the returned connection is considered "in use" until you close it, which means any subsequent requests to the *sql.DB
pool will need to open a new connection.:memory:
, each connection will refer to a separate in-memory database.You should instead use the memdb vfs so that different connections in the pool refer to the same in-memory database, and you should configure the pool to never close the last connection.
// Note that the path you give here is somewhat arbitrary, but it MUST start with a slash to work properly.
db, err := sql.Open("sqlite3", "file:/whatever?vfs=memdb")
if err != nil {
...
}
defer db.Close()
db.SetConnMaxIdleTime(0)
db.SetConnMaxLifetime(0)
db.SetMaxIdleConns(1)
db.SetMaxOpenConns(...)
if err := db.Ping(); err != nil {
...
}
...
I've tried to replace :memory:
into file:/foobar?vfs=memdb
in previous code, and it works.
Thanks a lot.
TL; DR
I've tried to restore an in-memory database from an exist sqlite database file, it returns no error but the in-memory db is empty.
However, if I changed
:memory:
topath/output.db
, it works.Environment
Database Schema
Code (in Go)
If I changed
toDb, err := sql.Open("sqlite3", ":memory:")
intotoDb, err := sql.Open("sqlite3", "another.db")
, it works well.Code (in C)
I've tried the same logic with sqlite C API, and it works well.