kuroneko / gosqlite3

Go Interface for SQLite3
http://kuroneko.github.com/gosqlite3
BSD 2-Clause "Simplified" License
83 stars 24 forks source link

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

Open vendion opened 12 years ago

vendion commented 12 years ago

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;
feyeleanor commented 12 years ago

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

feyeleanor commented 12 years ago

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?

DisposaBoy commented 12 years ago

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

vendion commented 12 years ago

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.