views:

48

answers:

1

I have three queries that I'm running through SQLite. These are what I'm running; the first is the table declaration and then the next 3 are the actual queries.

Declaration:

"CREATE TABLE IF NOT EXISTS items (busid INTEGER PRIMARY KEY, ipaddr TEXT, time TEXT DEFAULT (NOW()));"

Queries:

  1. (Works) "INSERT INTO items (time, ipaddr) VALUES ('test', '192.168.1.1');"
  2. (Crashes) "INSERT INTO items (busid, ipaddr) VALUES (10, '192.168.1.1');"
  3. (Crashes) "INSERT INTO items (ipaddr) VALUES ('192.168.1.1');"

Query 1 works fine, whereas queries 2 and 3 cause a crash by means of an EXC_BAD_ACCESS within sqlite3ExprCodeTarget. Tracing back, I discover the last point my code touches is a sqlite3_prepare_v2() statement, which passes a valid database/string/etc. and, as I said, works fine with statement 1 above but not 2 or 3. What's wrong with this, and how can I fix it? Thanks.

EDIT 1: To clarify, Query 1, 2, and 3 are run separately in different runs of the program, not in sequence, and the database is destroyed in between runs, so running Query 1 has no effect on running Query 2 after the reset.

EDIT 2: Example C++ source file showing issue: http://cl.ly/2w3c - just set the database path in the source code to wherever you want it to create the test database.

+1  A: 

i downloaded your source, compiled and ran with each line uncommented in turn, all works ok

sqlite version 3.6.23.1

redhat 5 32 bit

so i guess the answer is - upgrade to latest version of sqlite

pm100
pm100
Well, I tried the latest version of sqlite (I'm on Mac OS 10.6.4 and it comes bundled with 3.6.12) and the second two don't crash but also don't add the item; it seems the error is "unknown function: NOW()". So it seems the error is in the CREATE TABLE statement where I try to set the default value for time to NOW(). It turns out instead of `NOW()` I have to use `date('now')` in SQLite. That was incorrect SQL (in SQLite at least) and it was causing the crash instead of a proper error. Thanks for the help though; it turns out the new version helped debug, even though both of them run.
jfm429