views:

750

answers:

2

Hey,

I'm working with sqlite3 for the first time, and cannot get it to properly check a file before it opens it. So far, sqlite always returns OK on any file.

Also, the file name is a variable returned from the GTK file chooser. It returns an absolute path, I'm guessing this is not a problem.

Thanks for any help.

This is a snippet of the code:

int rc;
char *filename;
sqlite3 *db;

filename = gtk_file_chooser_get_filename(etc.);

if(SQLITE_OK == rc = sqlite3_open(filename,&db))
{  etc. }
A: 

Does your code compile?
I believe this is an error

if (SQLITE_OK == rc = sqlite3_open(filename,&db)) { /* ... */ }

it's the same as

if ((SQLITE_OK == rc) = sqlite3_open(filename,&db)) { /* ... */ }

and you cannot assign something (the result of the sqlite3_open() call) to (SQLITE_OK == rc).

Try this:

if ((rc = sqlite3_open(filename,&db)) == SQLITE_OK) { /* ... */ }
/* or, if you really want the constant on the left side of the comparison */
if (SQLITE_OK == (rc = sqlite3_open(filename,&db))) { /* ... */ }
pmg
Thanks for the tip. It does compile, but the example was poorly written. First rc and db are set to NULL, then the connection is performed. After that, the returned values are checked.
Shawn B
@laalto: That's what I said. `0 == x = fx()` gets parsed as `(0 == x) = fx()` and you can't assign to `(0 == x)`.
pmg
[ http://codepad.org/r7uQCSxZ ]
pmg
Yes, you're right.
laalto
+5  A: 

sqlite3_open doesn't actually read the file until the first non-pragma statement is prepared.

sqlite3_open_v2 provides other options.

Doug Currie