尝试打开SQLite3数据库时出现错误

时间:2018-08-23 20:30:33

标签: c++ visual-c++ sqlite

我正在Visual Studio Community 2017中工作,我想做的是打开并读取C ++中的数据库信息。

#include <stdio.h>
#include <string>
using std::string;
#include <sstream>
using std::stringstream;

#include "C:\Users\santiago.corso\Desktop\sqlite-amalgamation-3240000 (1)\sqlite-amalgamation-3240000\sqlite3.h"

bool find_employee(int _id)
{
    bool found = false;
    sqlite3* db;
    sqlite3_stmt* stmt;
    stringstream ss;

    // create sql statement string
    // if _id is not 0, search for id, otherwise print all IDs
    if (_id) { ss << "select * from employees where id = " << _id << ";"; }
    else { ss << "select * from employees;"; }
    string sql(ss.str());

    //the resulting sql statement
    printf("sql: %s\n", sql.c_str());

    //get link to database object
    if (sqlite3_open("C:\ProgramData\PROISER\ISASPSUS\datastore\dsfile.db", &db) != SQLITE_OK) {
        printf("ERROR: can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return found;
    }

    // compile sql statement to binary
    if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL) != SQLITE_OK) {
        printf("ERROR: while compiling sql: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        sqlite3_finalize(stmt);
        return found;
    }

    // execute sql statement, and while there are rows returned, print ID
    int ret_code = 0;
    while ((ret_code = sqlite3_step(stmt)) == SQLITE_ROW) {
        printf("TEST: ID = %d\n", sqlite3_column_int(stmt, 0));
        found = true;
    }
    if (ret_code != SQLITE_DONE) {
        //this error handling could be done better, but it works
        printf("ERROR: while performing sql: %s\n", sqlite3_errmsg(db));
        printf("ret_code = %d\n", ret_code);
    }

    printf("entry %s\n", found ? "found" : "not found");

    //release resources
    sqlite3_finalize(stmt);
    sqlite3_close(db);

    return found;
}

返回的错误来自“编译器错误C4129”类别。

我无法解决。如果您能帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我可以通过在sqlite3_open的路径的所有部分添加“ \\”来纠正上述错误。除此之外,关于

的必须定义的入口点弹出了另一个错误。
if (sqlite3_open("C:\ProgramData\PROISER\ISASPSUS\datastore\dsfile.db", &db) != SQLITE_OK) {
        printf("ERROR: can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return found;`

我想知道的是,用这段代码是否足以打开和读取数据库。我从sqlite3开始,接下来我要做的就是将其内容导出到Excel文件中。 有人建议我使用确切的应用程序来生成sqlite3本身的原因,就像cmd。但是,我宁愿为此目的构建脚本而不是使用cmd。