使用c ++打开sqlite数据库时出错

时间:2014-02-27 16:43:31

标签: c++ sqlite

我是在用c ++编写sqlite的例子 http://cplus.about.com/od/howtodothingsin1/a/tutorial-two-With-SQLite-and-c.htm

以下是我的代码的一部分:

#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include "sqlite3.h"

  sqlite3 *db;
  sqlite3_stmt *stmt;
  char message[255];

  // open the database                                                                                                                       
  int db_open = sqlite3_open(dbname, &db);
  if (db_open != SQLITE_OK)
    {
      cout << "Failed to open database " << sqlite3_errstr(db_open) << '\n';
      sqlite3_close(db);
      exit(0);
    }
  cout << "Opened db " << dbname << "OK\n";

  //prepare the sql                                                                                                                          
  int db_prepare = sqlite3_prepare_v2(db, sql, strlen(sql) + 1, &stmt, NULL);
  if (db_prepare != SQLITE_OK)
    {
      cout << "Failed to prepare database " << sqlite3_errstr(db_prepare) << '\n';
      sqlite3_close(db);
      exit(0);
    }

我收到此错误,建议我使用sqlite3_errmsg而不是sqlite3_errstr(确实存在)。那是为什么?

db2class.cpp:31:45: error: use of undeclared identifier 'sqlite3_errstr'; did you mean 'sqlite3_errmsg'?
      cout << "Failed to open database " << sqlite3_errstr(db_open) << '\n';
                                            ^~~~~~~~~~~~~~
                                            sqlite3_errmsg
/usr/include/sqlite3.h:2754:24: note: 'sqlite3_errmsg' declared here
SQLITE_API const char *sqlite3_errmsg(sqlite3*);
                       ^
db2class.cpp:31:60: error: cannot initialize a parameter of type 'sqlite3 *' with an lvalue of type 'int'
      cout << "Failed to open database " << sqlite3_errstr(db_open) << '\n';
                                                           ^~~~~~~
/usr/include/sqlite3.h:2754:47: note: passing argument to parameter here
SQLITE_API const char *sqlite3_errmsg(sqlite3*);
                                              ^
db2class.cpp:41:48: error: use of undeclared identifier 'sqlite3_errstr'; did you mean 'sqlite3_errmsg'?
      cout << "Failed to prepare database " << sqlite3_errstr(db_prepare) << '\n';
                                               ^~~~~~~~~~~~~~
                                               sqlite3_errmsg
/usr/include/sqlite3.h:2754:24: note: 'sqlite3_errmsg' declared here
SQLITE_API const char *sqlite3_errmsg(sqlite3*);
                       ^
db2class.cpp:41:63: error: cannot initialize a parameter of type 'sqlite3 *' with an lvalue of type 'int'
      cout << "Failed to prepare database " << sqlite3_errstr(db_prepare) << '\n';
                                                              ^~~~~~~~~~
/usr/include/sqlite3.h:2754:47: note: passing argument to parameter here
SQLITE_API const char *sqlite3_errmsg(sqlite3*);

2 个答案:

答案 0 :(得分:0)

教程告诉您下载最新的SQLite版本。

您没有这样做,因此您使用的是尚未拥有sqlite3_errstr功能的旧版本。

(BTW;在这种情况下使用sqlite3_errstr是错误的; sqlite3_errmsg会返回包含更多有用信息的消息。)

答案 1 :(得分:0)

检查sqlite版本。

添加sqlite3_errstr的版本是2012-12-12(3.7.15)版本。