C++使用VS+vcpkg+sqlpp11连接mysql
注1:MySQL自带的库连接数据库看着太不和谐,这里使用sqlpp11以及其配套的sqlpp11-connector-mysql[mysql]连接数据库
注2:sqlpp11-connector-mysql[mysql]安装有个坑,可以使用vcpkg install sqlpp11-connector-mysql[core,mysql]:x64-windows命令安装,详细参考https://github.com/microsoft/vcpkg/issues/12040
一、vcpkg配置以及与VS集成
详情参考官方文档:https://github.com/Microsoft/vcpkg#quick-start-windows或https://gitee.com/mirrors/vcpkg?_from=gitee_search
二、安装相关库
- 安装
sqlpp11-connector-mysql[core,mysql]:x64-windows
使用文档及示例:https://github.com/rbock/sqlpp11-connector-mysql或https://gitee.com/warm_dawn/sqlpp11-connector-mysql?_from=gitee_search
- 安装
sqlpp11:x64-windows
使用文档及示例:https://github.com/rbock/sqlpp11或https://gitee.com/mirrors/sqlpp11?_from=gitee_search
此库使用较为冗长,但还算较为好理解
三、集成进VS后直接在VS普通项目中使用
-
创建项目
-
创建测试表
-
项目位数与库保持一致
记得安装的库位数与项目一致
- 表描述代码table.h
#pragma once
#include "sqlpp11/table.h"
#include "sqlpp11/char_sequence.h"
#include "sqlpp11/column_types.h"
namespace test_table_
{
struct id_
{
struct _alias_t
{
static constexpr const char _literal[] = "id";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T id;
T& operator()()
{
return id;
}
const T& operator()() const
{
return id;
}
};
};
using _traits = ::sqlpp::make_traits<::sqlpp::integer>;
};
struct account_
{
struct _alias_t
{
static constexpr const char _literal[] = "account";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T account;
T& operator()()
{
return account;
}
const T& operator()() const
{
return account;
}
};
};
using _traits = ::sqlpp::make_traits<::sqlpp::varchar>;
};
struct password_
{
struct _alias_t
{
static constexpr const char _literal[] = "password";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T password;
T& operator()()
{
return password;
}
const T& operator()() const
{
return password;
}
};
};
using _traits = ::sqlpp::make_traits<::sqlpp::varchar>;
};
}
struct test_table : sqlpp::table_t<test_table, test_table_::id_, test_table_::account_, test_table_::password_>
{
using _value_type = sqlpp::no_value_t;
struct _alias_t
{
static constexpr const char _literal[] = "test_table";
using _name_t = sqlpp::make_char_sequence<sizeof(_literal), _literal>;
template <typename T>
struct _member_t
{
T tb;
T& operator()()
{
return tb;
}
const T& operator()() const
{
return tb;
}
};
};
};
- 查询代码main.cpp
#include <iostream>
#include <sqlpp11/sqlpp11.h>
#include <sqlpp11/mysql/mysql.h>
#include <iostream>
#include <memory>
#include "table.h"
using namespace sqlpp;
using namespace std;
std::shared_ptr <mysql::connection> connect_database()
{
auto config = std::make_shared<mysql::connection_config>();
//数据库地址
config->host = "127.0.0.1";
//端口号
config->port = 3306;
//数据库登陆账号
config->user = "root";
//数据库登陆密码
config->password = "root";
//要操作的数据库
config->database = "test";
// config->debug = true;
return std::make_shared<mysql::connection>(config);
}
int main() {
auto db = connect_database();
test_table tb{};
uint32_t limit{ 1 };
const auto& row = db->run(sqlpp::select(all_of(tb)).from(tb).unconditionally());
if (!row.empty()) {
const auto& rs = row.front();
cout << rs.id << "\t" << rs.account.text << "\t" << rs.password.text << "\n";
}
return 0;
}
- 测试数据
- 连接使用