SQLite Crash on Inserting Items
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:
- (Works) "INSERT INTO items (time, ipaddr) VALUES ('test', '192.168.1.1');"
- (Crashes) "INSERT INTO items (busid, ipaddr) VALUES (10, '192.168.1.1');"
- (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:
#include <iostream>
using namespace std;
#include <sqlite3.h>
int main (int argc, const char * argv[]) {
sqlite3 *db;
sqlite3_open("/Users/Justin/Desktop/test.db", &db);
string createQuery = "CREATE TABLE IF NOT EXISTS items (busid INTEGER PRIMARY KEY, ipaddr TEXT, time TEXT NOT NULL DEFAULT (NOW()));";
sqlite3_stmt *createStmt;
cout << "Creating Table Statement" << endl;
sqlite3_prepare_v2(db, createQuery.c_str(), createQuery.size(), &createStmt, NULL);
cout << "Stepping Table Statement" << endl;
if (sqlite3_step(createStmt) != SQLITE_DONE) cout << "Didn't Create Table!" << endl;
// COMMENT OUT UNUSED ONES
// First one works, second and third ones fail.
string insertQuery = "INSERT INTO items (time, ipaddr) VALUES ('test', '192.168.1.1');"; // WORKS!
//string insertQuery = "INSERT INTO items (busid, ipaddr) VALUES (10, '192.168.1.1');"; // FAILS!
//string insertQuery = "INSERT INTO items (ipaddr) VALUES ('192.168.1.1');"; // FAILS!
sqlite3_stmt *insertStmt;
cout << "Creating Insert Statement" << endl;
sqlite3_prepare_v2(db, insertQuery.c_str(), insertQuery.size(), &insertStmt, NULL);
cout << "Stepping Insert Statement" << endl;
if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl;
cout << "Success!" << endl;
}
Just set the database path in the source code to wherever you want it to create the test database.
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
精彩评论