mattn / go-sqlite3

sqlite3 driver for go using database/sql

Home Page:http://mattn.github.io/go-sqlite3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

limoges opened this issue · comments

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
}

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

    db, err := sql.Open("sqlite3", "file:locked.sqlite?cache=shared&mode=rwc")

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

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.

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

@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.

You need to build go from source code.

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