【问题标题】:How do I connect to an sqlite database in C++ using visual studio 2017 on Windows?如何在 Windows 上使用 Visual Studio 2017 连接到 C++ 中的 sqlite 数据库?
【发布时间】:2018-10-27 23:43:49
【问题描述】:

我对 Windows 开发和 C++ 还很陌生。在尝试连接到 sqlite 数据库时,我编写了以下 sn-p。

#include "stdafx.h"
#include "sqlite3.h"

int main()
{
    sqlite3* db;

    if (sqlite3_open(<path to db>, &db) != SQLITE_OK) {
        printf("ERROR: can't open database: %s\n", sqlite3_errmsg(db));
    }
    else { printf("Connection Successful"); }

    sqlite3_close(db);

    return 0;
}

我从http://www.sqlite.org/download.html 下载了通用 Windows 平台 VSIX 包,其中包括一个 sqlite3.lib 文件和一个 sqlite3.dll。在 Microsoft Visual Studio 2017 中,我已经包含了 sqlite3.lib 文件和目录,并将 sqlite3.dll 复制到项目目录中。

我应该注意包含的 sqlite3.lib 是 x86 版本。尝试包含 x64 版本会导致 '#include "sqlite3.h"' 语句的构建错误。我想知道为什么会这样,但我现在可以接受 32 位。

按原样运行代码会在运行时产生以下错误:“代码执行无法继续,因为未找到 VCRUNTIME140_APP.dll...”

This link 建议 dll 应该包含在 Visual Studio 安装中。提到的 dll 文件确实存在于 C:\Program Files (x86) 的多个位置,包括 Microsoft SDKs 文件夹,.lib 文件位于该文件夹下。为什么找不到?

【问题讨论】:

  • 您是使用 MSVS 项目向导创建 Windows Store 应用程序(通用应用程序)还是标准 Win32 控制台应用程序?你不能混搭。
  • 这实际上可能是问题所在,谢谢。我会检查其他下载是否有效。
  • @RichardCritten:您可以在常规桌面应用程序中使用绝大多数通用 Windows 平台 API。除了少数例外,您可以混搭。

标签: c++ dll sqlite visual-studio-2017 visual-c++


【解决方案1】:

SQLite 将在 msvc 下轻松编译。所以你可以在msvc中直接将sqlite3.h和sqlite3.cpp包含到你的项目中,避免作为依赖库使用。

【讨论】:

    【解决方案2】:

    下载以下文件夹并将其复制到您的 Visual C++ 解决方案文件夹中。
    https://github.com/mcychan/DNAssist/tree/master/sqlite3

    sqlite3 文件夹包含 x86 和 x64 版本的 sqlite dll 和 lib。 您可以升级到最新版本的 sqlite.dll。

    下载以下文件并将其复制到您的 Visual C++ 项目文件夹中,为其添加引用。
    https://github.com/mcychan/DNAssist/blob/master/DNAssist/CppSQLite3.cpp
    https://github.com/mcychan/DNAssist/blob/master/DNAssist/CppSQLite3.h

    以下是查询数据库的示例代码。

    #include "CppSQLite3.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    CppSQLite3DB db;
    
    bool GetDatabase(const string& dbPath)
    {
        try {
            db.open(dbPath.c_str());
            return true;
        }
        catch (CppSQLite3Exception& e)
        {
            cout << _T("Cannot open database: ") << dbPath << _T("\n");
            return false;
        }
    }
    
    void IssueQuery(const string& querystring, const string& field1)
    {
        try {
            CppSQLite3Query q = db.execQuery(querystring.c_str());
            while (!q.eof()) {
                CString temp2(q.fieldValue(field1.c_str()));
                TRACE(temp2 + _T("\n"));
                q.nextRow();
            }
        }
        catch (CppSQLite3Exception& e)
        {
            cout << _T("Cannot execute query: ") << querystring << _T("\n");
        }
    }
    
    void main()
    {
        if(GetDatabase("C:\\test.sqlite"))
            IssueQuery("SELECT * FROM DUAL", "X");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      相关资源
      最近更新 更多