Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

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

limoges opened this issue Feb 12, 2013 · 6 comments

Comments

@limoges
Copy link

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
Copy link
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
Copy link
Author

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.

@tzngit
Copy link

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
Copy link
Owner

mattn commented Apr 1, 2013

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

@tzngit
Copy link

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
Copy link
Owner

mattn commented Apr 1, 2013

You need to build go from source code.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants