Cannot insert while select is active (same or different table) #39

Closed
limoges opened this Issue Feb 12, 2013 · 6 comments

Projects

None yet

3 participants

@limoges
limoges commented Feb 12, 2013

Cannot insert or update while a query is "opened", which is not prohibited by the SQLite api.

Opening the database with SQLITE_OPEN_NOMUTEX or SQLITE_OPEN_PRIVATECACHE does not fix the problem.

This code while always result in golang "could not insert data into dst: database is locked". When one writes the equivalent code in C, the program just works.

I am about lost as to how to fix the problem. This "kind of use" has been supported by SQLite for quite some time now: database is locked.

package main

import (
    "database/sql"
    _ "github.com/mattn/go-sqlite3"
    "log"
)

func main() {

    db, err := sql.Open("sqlite3", "locked.sqlite")
    if err != nil {
        log.Fatalln("could not open database:", err)
    }
    defer db.Close()

    // Drop, create and insert data into the test table
    _, err = db.Exec("DROP TABLE IF EXISTS src;")
    if err != nil {
        log.Fatalln("could not drop table:", err)
    }
    _, err = db.Exec("DROP TABLE IF EXISTS dst;")
    if err != nil {
        log.Fatalln("could not drop table:", err)
    }

    _, err = db.Exec("CREATE TABLE src (id INTEGER NOT NULL PRIMARY KEY, data1 INTEGER, data2 INTEGER);")
    if err != nil {
        log.Fatalln("could not create table:", err)
    }
    _, err = db.Exec("CREATE TABLE dst (id INTEGER, data1 INTEGER, data2 INTEGER);")
    if err != nil {
        log.Fatalln("could not create table:", err)
    }

    for i := 0; i < 100; i++ {
        _, err = db.Exec("INSERT INTO src (id, data1, data2) VALUES (?, ?, ?);", i, 100 + i, 1000 + i)
        if err != nil {
            log.Fatalln("could not insert into table:", err)
        }
    }

    rows, err := db.Query("SELECT id, data1, data2 FROM src;")
    if err != nil {
        log.Fatalln("could not select data:", err)
    }
    defer rows.Close()

    insert, err := db.Prepare("INSERT INTO dst (id, data1, data2) VALUES (?, ?, ?);")
    if err != nil {
        log.Fatalln("could not prepare statement:", err)
    }
    defer insert.Close()

    for rows.Next() {
        var id, data1, data2 int64

        err = rows.Scan(&id, &data1, &data2)
        if err != nil {
            log.Fatalln("could not scan row:", err)
        }

        _, err = insert.Exec(id, data1, data2)
        if err != nil {
            log.Fatalln("could not insert data into dst:", err)
        }
    }
    insert.Close()
    rows.Close()

    return
}
@mattn
Owner
mattn commented Feb 13, 2013

If you have latest sqlite3 and go-sqlite3, try to use:

    db, err := sql.Open("sqlite3", "file:locked.sqlite?cache=shared&mode=rwc")
@limoges
limoges commented Feb 13, 2013

Wow thank you. I got the latest versions, fixed pkg-config stuff and yes, it does indeed work as intended now. Weird.

@limoges limoges closed this Feb 13, 2013
@mattn mattn added a commit that referenced this issue Feb 13, 2013
@mattn Updated doc. Added link to #27 #39 716b2f5
@tzngit
tzngit commented Apr 1, 2013

I built go-sqlite3.go and I could see the go-sqlite3.a in my GOPATH/pkg. But when I built the example, I got:

command-line-arguments

D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __moddi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __moddi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __moddi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __moddi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __moddi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __moddi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __umoddi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __udivdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __umoddi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __udivdi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __umoddi3: not defined
D:\tzn\bishe\GoSmp\pkg\windows_386/github.com/mattn/go-sqlite3.a(sqlite3_windows.)(.text): __divdi3: not defined

give me a hand,thanks.

@mattn
Owner
mattn commented Apr 1, 2013

@tzngit probably, you are using 64bit OSs. #27
please try to upgrade go to latest.

@tzngit
tzngit commented Apr 1, 2013

@mattn I am in window 32bit ,using go1.0.3 and sublimetext 2.0,I have the sqlite3.dll but I don't know where to put it or how to use it.

@mattn
Owner
mattn commented Apr 1, 2013

You need to build go from source code.

http://golang.org/doc/install/source

@nealjc nealjc added a commit to nealjc/ipreg that referenced this issue Jan 17, 2014
@nealjc nealjc Work-around for mattn/go-sqlite3#39 e88336b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment