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
注2图片

一、vcpkg配置以及与VS集成

详情参考官方文档:https://github.com/Microsoft/vcpkg#quick-start-windows或https://gitee.com/mirrors/vcpkg?_from=gitee_search
在这里插入图片描述

二、安装相关库

  1. 安装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

在这里插入图片描述

  1. 安装sqlpp11:x64-windows
    使用文档及示例:https://github.com/rbock/sqlpp11或https://gitee.com/mirrors/sqlpp11?_from=gitee_search
    此库使用较为冗长,但还算较为好理解
    在这里插入图片描述

三、集成进VS后直接在VS普通项目中使用

  1. 创建项目
    在这里插入图片描述

  2. 创建测试表
    在这里插入图片描述

  3. 项目位数与库保持一致
    在这里插入图片描述

记得安装的库位数与项目一致

  1. 表描述代码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;
			}
		};
	};
};

  1. 查询代码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;
}
  1. 测试数据
    在这里插入图片描述
  2. 连接使用
    在这里插入图片描述
  • 12
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值