[C++] MySQL ์ฐ๋ in Visual Studio
๋ณดํต mysql์ ๊ธฐ๋ณธ์ ์ผ๋ก ๊น๋ ค์๋๊ฑด ๊ทธ๋ฅ C๋ฒ์ API๋ค.
๊ทธ๋ฅ C๋ฒ์ ์ ์จ๋ ๋๊ธด ํ๋๋ฐ, ์ด์ C++์ ์ธ๊ฑฐ๋ฉด ์ ๋๋ก ์จ์ผํ์ง ์๊ฒ ๋๊ฐ?
์ผ๋จ ์ฌ๊ธฐ์ C++๋ฒ์ ์ปค๋ฅํฐ๋ฅผ ๋ค์ด๋ฐ์ผ๋ฉด ๋๋ค.
https://dev.mysql.com/downloads/connector/cpp/8.0.html
๋๋ 64 ๋ฒ์ ์ผ๋ก ๊น๊ณ ๊ทธ๋ฅ ๊ธฐ๋ณธ์ค์ ์ผ๋ก ๊น์๋ค.
๊ทธ๋ผ ์์ชฝ ํ๋ก๊ทธ๋จ ํ์ผ์ฆ์ MySQL ๋๋ ํฐ๋ฆฌ์ ์ด๋ ๊ฒ ์๊ธธ๊ฑฐ๋ค.
์ฌ๊ธธ ๋์ถฉ ์ฐ์ด๋์.
๋ฐ๋ก๊ฐ๊ธฐ๋ฅผ ๋ง๋ค์ด์ ์ด๋์ ๋ชจ์๋๋ ๊ฐ์.
์ ๊ทธ๋ฆฌ๊ณ , ์ด C++ ์ปค๋ฅํฐ๋ boost ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ข
์์ ์ด๋ค.
boost๋ฅผ ๋จผ์ ์ค์นํ๊ณ ์ธํ
ํ ๊ฒ์ ์๋ฆฐ๋ค.
๋๋ ๋น์ฃผ์ผ ์คํ๋์ค์์ ์์ ์ ํ๋๋ฐ.. ์ํ๋ค๋ฉด ๋ค๋ฅธ๊ฑธ ์จ๋ ๋๋ค.
์ฌํผ ๋๋ dll ๋ฌ๊ณ ๋ค๋๋๊ฒ ์ซ์ด์ ์ ์ ์ผ๋ก ๋งํฌ๋ฅผ ํ ๊ฒ์ด๋ค.
๊ทผ๋ฐ ์ ์ ๋ฒ์ ์ ๋๋ฒ๊น
๋ชจ๋์์๋ ์ ๋๋๊น, ๋ฆด๋ฆฌ์ฆ ๋ชจ๋๋ก ํด์ผํ๋ค.
sfml์ static ๋ฒ์ ๋ ๋๋ฒ๊น
๋ชจ๋๊ฐ ์์๋๋ฐ... ์ ์ฑ์ด ๋ถ์กฑํ ๊ฒ ๊ฐ๋ค.
์ฌํผ ํ๋ก์ ํธ ์์ฑ์ ๋ค์ด๊ฐ์ ๋ฆด๋ฆฌ์ฆ 64๋นํธ๋ก ๋ฐ๊พธ๊ณ ,
ํฌํจ ๋๋ ํฐ๋ฆฌ์ boost์ ์ปค๋ฅํฐ ํค๋ ์์น๋ฅผ ์ถ๊ฐํด์ค๋ค.

์ถ๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋๋ ํ ๋ฆฌ์๋ ์ปค๋ฅํฐ์ ๋ถ์คํธ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฒฝ๋ก๋ฅผ ๋ฃ์ด์ค๋ค.

์ถ๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ข ์์ฑ์๋ ์ ๋๋์ ๋ฃ์ด์ค๋ค.

๋ง์ฝ dll์ด๋ ์ฐ๊ฒฐํ๊ณ ์ถ๋ค๋ฉด, ์ ๊ธฐ์ static๋ง ์๋ ๋ฒ์ ์ด ์๋ค. ๊ทธ๊ฑธ ๋ฌ๋ฉด ๋๋ค.
์ฝ๋๋ ์ด๋ ๊ฒ ์ง๋ฉด ๋๋ค.
์๋ ์ฝ๋๋ ๊ทธ๋ฅ ๋๋น์ ์๋๊ฑธ select๋ก ์ฝ๊ธฐ๋ง ํ๋ค. ๋ฐ์ดํฐ๋ ๋ฏธ๋ฆฌ ๋ฃ์ด๋๋ค.
#include "pch.h"
#include <iostream>
#include <memory>
#define STATIC_CONCPP //static ๋ฒ์ ์ด๋ผ๋ ๊ฑธ ์๋ฆผ!
#include <mysql/jdbc.h>
int main()
{
constexpr auto host_name = "localhost";
constexpr auto username = "admin";
constexpr auto password = "1234";
try {
auto driver = get_driver_instance(); //์ ํด์ ํ๋ฉด ์๋จ
auto connector = std::unique_ptr<sql::Connection>(driver->connect(host_name, username, password)); //์ฐ๊ฒฐ
connector->setSchema("TestDB"); //ํน์ DB ์ ์
auto statement = std::unique_ptr<sql::Statement>(connector->createStatement());
//์ฟผ๋ฆฌ ๋ณด๋ด๊ณ ๋ฐ์์ด
auto result_set = std::unique_ptr<sql::ResultSet>(statement->executeQuery("SELECT * from PeopleTable"));
//๊ณ์ ์ถ๋ ฅ
while (result_set->next())
std::cout << result_set->getString("id") << ' ' << result_set->getString("name") << ' ' << result_set->getString("age") << std::endl;
}
catch (sql::SQLException& e) {
std::cout << e.what() << std::endl
<< "error code: " << e.getErrorCode() << std::endl
<< "sqlstate:" << e.getSQLState() << std::endl;
}
}
์ ๋๋ค.