[英]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.