简体   繁体   English

如何在 Windows 上使用 Visual Studio 2017 连接到 C++ 中的 sqlite 数据库?

[英]How do I connect to an sqlite database in C++ using visual studio 2017 on Windows?

I am fairly new to Windows development and C++.我对 Windows 开发和 C++ 相当陌生。 In trying to connect to an sqlite database, I've written the following snippet.在尝试连接到 sqlite 数据库时,我编写了以下代码段。

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

I downloaded the Universal Windows Platform VSIX package from http://www.sqlite.org/download.html , which included a sqlite3.lib file and a sqlite3.dll.我从http://www.sqlite.org/download.html下载了通用 Windows 平台 VSIX 包,其中包括一个 sqlite3.lib 文件和一个 sqlite3.dll。 In Microsoft Visual Studio 2017, I have included the sqlite3.lib file and directories and copied the sqlite3.dll to the project directory.在 Microsoft Visual Studio 2017 中,我包含了 sqlite3.lib 文件和目录,并将 sqlite3.dll 复制到了项目目录中。

I should note the included sqlite3.lib is the x86 version.我应该注意到包含的 sqlite3.lib 是 x86 版本。 Trying to include the x64 version causes a build error with the '#include "sqlite3.h"' statement.尝试包含 x64 版本会导致使用 '#include "sqlite3.h"' 语句生成构建错误。 I'd like to know why that's the case, but I can live with 32-bit for now.我想知道为什么会这样,但我现在可以使用 32 位。

Running the code as is yields the following error at runtime: "The code execution cannot proceed because VCRUNTIME140_APP.dll was not found..."按原样运行代码会在运行时产生以下错误:“代码执行无法继续,因为未找到 VCRUNTIME140_APP.dll...”

This link suggests that the dll should have been included with the visual studio install. 此链接表明 dll 应该已包含在 Visual Studio 安装中。 The mentioned dll file does exist in C:\\Program Files (x86) in several places including the Microsoft SDKs folder, under which the .lib file resides.提到的 dll 文件确实存在于 C:\\Program Files (x86) 中的多个位置,包括 Microsoft SDKs 文件夹,.lib 文件位于该文件夹下。 Why may it not be found?为什么可能找不到?

SQLite will compile under msvc without a hassle. SQLite 将在 msvc 下轻松编译。 So you can directly include sqlite3.h and sqlite3.cpp into your project in msvc and avoid using as dependency library.所以你可以直接在 msvc 中包含 sqlite3.h 和 sqlite3.cpp 到你的项目中,避免用作依赖库。

Download and copy the following folder to your Visual C++ solution folder.下载以下文件夹并将其复制到 Visual C++ 解决方案文件夹。
https://github.com/mcychan/DNAssist/tree/master/sqlite3 https://github.com/mcychan/DNAssist/tree/master/sqlite3

The sqlite3 folder contains both x86 and x64 version of sqlite dll and lib. sqlite3 文件夹包含 x86 和 x64 版本的 sqlite dll 和 lib。 You may upgrade to latest version of sqlite.dll.您可以升级到最新版本的 sqlite.dll。

Download and copy the following files to your Visual C++ project folder, add reference for them.下载以下文件并将其复制到您的 Visual C++ 项目文件夹,为它们添加引用。
https://github.com/mcychan/DNAssist/blob/master/DNAssist/CppSQLite3.cpp https://github.com/mcychan/DNAssist/blob/master/DNAssist/CppSQLite3.cpp
https://github.com/mcychan/DNAssist/blob/master/DNAssist/CppSQLite3.h https://github.com/mcychan/DNAssist/blob/master/DNAssist/CppSQLite3.h

The following is the sample code to query the database.以下是查询数据库的示例代码。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C ++在Visual Studio 2017中修复此TextureBackground标识符 - How do I fix this textureBackground identifier in Visual Studio 2017 using C++ 如何在 Visual Studio 2017 中无错误地打开 Visual Studio 2019 c++ 代码? - How do i open Visual Studio 2019 c++ code in Visual Studio 2017 without errors? 我需要哪个SDK才能确保Visual Studio C ++ 2017中的Windows 7兼容性 - Which SDK do I need to ensure Windows 7 compatibility in Visual Studio C++ 2017 如何在Visual Studio 2017中将Windows控件添加到C ++桌面应用程序中? - How can I add windows controls to a C++ desktop application in Visual Studio 2017? 如何在 Visual Studio 2017 中为 C++ 控制台应用程序添加 header 文件 - How do I add header file for a C++ console app in Visual Studio 2017 如何使用 Visual Studio 2017 在 C++ 中为参数化对象数组使用唯一指针? - How can I use unique pointers for an array of parameterized objects in C++ using Visual Studio 2017? 如何将Visual Studio for Windows中制作的C ++解决方案转移到Visual Studio for Mac? - How do I transfer my C++ solution made in Visual Studio for Windows to Visual Studio for Mac? 我应该将Windows 8.1 SDK或Windows 10 SDK与Visual Studio 2017一起用于标准C ++吗? - Should I use Windows 8.1 SDK or Windows 10 SDK with Visual Studio 2017 for standard C++? 如何使用 c++ 和使用 Visual Studio 2017 菜单编辑器创建带有 Windows 菜单的 win32 应用程序 - how to create a win32 application with Windows menus using c++ and using the visual studio 2017 menu editor 如何使用C ++将MS Access 2013数据库连接到Visual Studio 2013 - How to connect ms access 2013 database to visual studio 2013 using c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM