关于插入项目的SQLite崩溃

时间:2010-10-21 19:33:45

标签: c++ database sqlite

我有三个查询,我正在通过SQLite运行。这些是我正在运行的;第一个是表声明,然后接下来的3个是实际查询。

宣言:

  

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

查询:

  
      
  1. (作品)“INSERT INTO items(time,ipaddr)VALUES('test','192.168.1.1');”
  2.   
  3. (崩溃)“INSERT INTO items(busid,ipaddr)VALUES(10,'192.168.1.1');”
  4.   
  5. (崩溃)“INSERT INTO items(ipaddr)VALUES('192.168.1.1');”
  6.   

查询1工作正常,而查询2和3通过sqlite3ExprCodeTarget中的EXC_BAD_ACCESS导致崩溃。追溯,我发现我的代码触及的最后一点是sqlite3_prepare_v2()语句,它传递一个有效的数据库/字符串/ etc。并且,正如我所说,上述声明1但不是2或3可以正常工作。这有什么问题,我该如何解决?感谢。

编辑1:为了澄清,查询1,2和3分别在程序的不同运行中运行,而不是按顺序运行,并且数据库在运行之间被销毁,因此运行查询1对运行查询2没有影响重置后。

编辑2:显示问题的示例C ++源文件:

#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;
}

只需将源代码中的数据库路径设置为您希望它创建测试数据库的任何位置。

1 个答案:

答案 0 :(得分:2)

我下载了您的源代码,编译并依次取消注释每一行,所有工作正常

sqlite版本3.6.23.1

redhat 5 32位

所以我想答案是 - 升级到最新版本的sqlite