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

Unclear behavior

Open wuzzapcom opened this issue 9 years ago • 7 comments

First of all, there is really big chance that i didn`t understand something. But i have problem with testing my code using this library. So, I have db users which I clean at the beginning of every test with code like this:

    	users, err := sql.Open("sqlite3", "users.db")
	if err != nil{
		log.Panic(err)
	}

	statement, err := users.Prepare("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, login TEXT, password TEXT, privateRSAKey TEXT, recordIDs TEXT)")
	if err != nil{
		log.Panic(err)
	}

	statement.Exec()
       	statement, err = users.Prepare("DROP TABLE users")
	if err != nil{
		log.Panic(err)
	}
	statement.Exec()
       	users.Close()

When I run every test manually everything is ok, tests pass. But when I start tests with

        go test

then the database is not cleared. I had idea that tests runs concurrently by default, but it is not true as i understood. And one more question about this lib : is it supposed to have 2 connections to db at the same time? For example, I open connection, write something, then in other place I open another connection and write something. Does it considered as multithreaded writing? Does lib flush data instantly?

wuzzapcom avatar Aug 10 '17 13:08 wuzzapcom

please check all errors.

mattn avatar Aug 10 '17 13:08 mattn

@mattn thank you, but i have no errors at all during execution and and every error is handled in project. Or did you said about some other errors?

wuzzapcom avatar Aug 10 '17 14:08 wuzzapcom

the code that you provided above seems not handle errors.

statement.Exec()

right?

mattn avatar Aug 10 '17 14:08 mattn

Oh, damn. Sorry, I've missed that this method returns something.

wuzzapcom avatar Aug 10 '17 14:08 wuzzapcom

So I checked and my error is

panic : database is locked[recovered]
    panic: database is locked

wuzzapcom avatar Aug 10 '17 14:08 wuzzapcom

So you should add mutex as err says. Or add query parameter _busy_timeout for connection string.

mattn avatar Aug 10 '17 15:08 mattn

Thank you, i got it.

wuzzapcom avatar Aug 10 '17 15:08 wuzzapcom