用 C++ 連接 MariaDB 資料庫 – 使用 MariaDB Connector/C++

環境
windows 11 64bit
Visual Studio 2022
遠端主機 cloudways
MariaDB 10.4

1. 在 cloudways 架 php 伺服器

在 cloudways 設定資料庫和建立資料表

將自己的 IP 加入白名單,這很重要!!!!

將自己的 IP 加入白名單,這很重要!!!!

將自己的 IP 加入白名單,這很重要!!!!

從這裡進入資料庫

在這裡輸入 SQL 指令建立資料表

SQL 指令

-- ----------------------------
-- Table structure for test01
-- ----------------------------
DROP TABLE IF EXISTS `test01`;
CREATE TABLE `test01`  (
  `id` int(11) NOT NULL,
  `int_test` int(11) NOT NULL,
  `char_test` char(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `varchar_test` varchar(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `text_test` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of test01
-- ----------------------------
INSERT INTO `test01` VALUES (1, 10, 'a', 'Harry Potter', 'The novels chronicle the lives of a young wizard, Harry Potter, whom are students at Hogwarts School of Witchcraft and Wizardry.');
INSERT INTO `test01` VALUES (2, 20, 'b', '哈利波特', '是一部兒童奇幻文學系列小說,描寫主角哈利波特在霍格華茲魔法學校7年學習生活中的冒險故事。');
INSERT INTO `test01` VALUES (3, 30, 'c', 'ハリー・ポッター', '1990年代のイギリスを舞台に、魔法使いの少年ハリー・ポッターの学校生活。');

SET FOREIGN_KEY_CHECKS = 1;

2. 安裝 MariaDB 函式庫

在這裡下載 MariaDB 函式庫
https://mariadb.com/downloads/#connectors

預設會安裝在 C:\Program Files\MariaDB

3. Visual Studio 2019

設定為 x64

去 C:\Program Files\MariaDB\MariaDB C++ Connector 64-bit 資料夾
mariadbcpp.dll 拿出來跟執行檔放在一起

.

main.cpp

#include <iostream>
using namespace std;
#include <C:/Program Files/MariaDB/MariaDB C++ Connector 64-bit/include/mariadb/conncpp.hpp>

#ifdef _WIN64
#pragma comment(lib,"C:\\Program Files\\MariaDB\\MariaDB C++ Connector 64-bit\\mariadbcpp.lib")
#else
#endif

sql::SQLString host("jdbc:mariadb://phpstack-XXX-XXX.cloudwaysapps.com:3306");
sql::SQLString database("database");
sql::SQLString username("username");
sql::SQLString password("mypassname");

int main()
{
	// Instantiate Driver
	sql::Driver* driver = sql::mariadb::get_driver_instance();

	std::unique_ptr<sql::Connection> conn(driver->connect(host + "/" + database, username, password));

	if (!conn)
	{
		cout << "Invalid database connection" << endl;
		system("pause");
		return 0;
	}

	// Create a new Statement
	std::unique_ptr<sql::Statement> stmnt(conn->createStatement());

	// Change character
	stmnt->execute("SET character set utf8");

	// Execute query
	sql::ResultSet* res = stmnt->executeQuery("select * from test01");

	// Loop through and print results
	while (res->next())
	{
		int nID = res->getInt("id");
		int nIntTest = res->getInt("int_test");
		sql::SQLString strChar = res->getString("char_test");		//這個取出來會是 utf8 字串
		sql::SQLString strVarChar = res->getString("varchar_test");	//這個取出來會是 utf8 字串
		sql::SQLString strText = res->getString("text_test");		//這個取出來會是 utf8 字串

		cout << "id = " << nID << endl;
		cout << "intTest = " << nIntTest << endl;
		cout << "char = " << strChar << endl;
		cout << "varChar = " << strVarChar << endl;//utf8 字串直接印會是亂碼

		string strChar2(strChar.c_str());
		string strVarChar2(strVarChar.c_str());
		string strText2(strText.c_str());

		//這些是我自己寫的函式庫,可以把 utf8 改成 unicode 並印出
		wstring strUniChar = hkstr::UTF8ToUnicode(strChar2);
		wstring strUniVarChar = hkstr::UTF8ToUnicode(strVarChar2);
		wstring strUniText = hkstr::UTF8ToUnicode(strText2);
		hkstr::PrintfUnicode(L"uni_char = %s\n", strUniChar.c_str());
		hkstr::PrintfUnicode(L"uni_varChar = %s\n", strUniVarChar.c_str());
		hkstr::PrintfUnicode(L"uni_text = %s\n", strUniText.c_str());

		cout << endl;
	}

	delete res;

	conn->close();

	system("pause");
	return 0;
}

執行結果

參考網址
https://mariadb.com/downloads/#connectors
https://mariadb.com/docs/clients/mariadb-connectors/connector-cpp/connect/
https://mariadb.com/resources/blog/how-to-connect-c-programs-to-mariadb/
https://mariadb.com/docs/clients/mariadb-connectors/connector-cpp/sample-app/
https://blog.csdn.net/wpc320/article/details/4822495

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *