2014-02-27 33 views
0

我是按照C++編程源碼 http://cplus.about.com/od/howtodothingsin1/a/tutorial-two-With-SQLite-and-c.htm錯誤使用C++

這裏是我的代碼部分的例子打開SQLite數據庫:

#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*); 
+0

請參閱[此鏈接](https://github.com/mattn/go-sqlite3/issues/70) –

回答

0

本教程告訴您下載最新的SQLite版本。

您沒有這樣做,所以您正在使用舊版本,該版本尚未具有sqlite3_errstr功能。

(順便說一句,使用sqlite3_errstr是錯在這種情況下;返回sqlite3_errmsg與更多的有用的信息的消息。)

0

檢查源碼的版本。

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

+0

這是準確和有用的,所以非常感謝。在http://sqlite.org/changes.html驗證了這一點(並且只搜索「sqlite3_errstr」)。 –