gosqlite3 icon indicating copy to clipboard operation
gosqlite3 copied to clipboard

sqlite3.Open errors if the the database being opened already exists

Open vendion opened this issue 13 years ago • 4 comments

Using the sample code in this Gist https://gist.github.com/1901836 using go version weekly.2012-02-14 +9e53309dfa6d

If the database doesn't exist when sqlite3.Open() gets called gosqlite3 creates the database file just fine, but if the same code is reran while the database file still exits err returns non nill and the error that is returned is:

go run test.go
panic: SQL error or missing database

goroutine 1 [running]:
main.main()
    /home/vendion/test.go:16 +0x127
exit status 2

I'm not sure if this is an error with gosqlite3 or not, but its not an issue with the database going bad as I can open the database with sqlite3 and the data is intact.

sqlite3 test.db                                                                                                                                                                             1 ?
SQLite version 3.7.10 2012-01-16 13:28:40
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE foo (id INTEGER PRIMARY KEY ASC, name VARCHAR(10));
INSERT INTO "foo" VALUES(1,'John');
COMMIT;

vendion avatar Feb 24 '12 16:02 vendion

I'll add a test and the appropriate fix once I've figured it out.

feyeleanor avatar Feb 27 '12 17:02 feyeleanor

I've added a test which I believe is equivalent to your case, but I can't replicate the problem on my dev system. Can you try the latest commit and report back on the results?

feyeleanor avatar Feb 27 '12 17:02 feyeleanor

I haven't done more than verify my hunch but I don't think this is a bug...

on line 14 you have:

_, err = db.Execute( "CREATE TABLE foo (id INTEGER PRIMARY KEY ASC, name VARCHAR(10));" )

this attempts to create the table which works fine the first time. It's not supposed to work a second time because the table already exists.

what you probably wanted was:

CREATE TABLE IF NOT EXISTS foo ...

also note, you will still get a panic because you're inserting a row with a fixed id (1) which already exists on the second run

DisposaBoy avatar Mar 04 '12 07:03 DisposaBoy

Sorry if the bug report was a bit confusing, I was in a hurry when writing it. The main issue is that the error that the package kicks back SQL error or missing database isn't all that helpful in knowing what is going on, this may cause an issue with more complex queries where someone may have a Syntax error or some other kind of mistake.

vendion avatar Mar 04 '12 14:03 vendion