Develop
[C++] Kali Linux에서 Mysql 연동해서 사용하기.
Dork94
2017. 12. 13. 14:13
안녕하세요.
오늘은 Mysql을 C++을 통해 사용하는 방법에 대해 알아보도록 하겠습니다.
우선 설정은 이전 포스팅에 관련 내용을 다루었으니 참고 바랍니다.
2017/12/13 - [Linux] - Kali Linux에서 Mysql 설치 및 C++ 환경 설정(Mysql root password setting)
//SqlMagician.h
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <iostream>
using namespace std;
class SqlMagician
{
sql::Driver *driver;
sql::Connection *conn;
sql::Statement *stmt;
sql::ResultSet *res;
string insertCmd="INSERT INTO ";
public:
SqlMagician(string domain,string user, string passwd);
SqlMagician(string domain,string user, string passwd,string dbName);
~SqlMagician();
void useDatabase(string database);
void insertSql(string tbName,string values);
};
//SqlMagician.cpp
#include "sqlmagician.h"
SqlMagician::SqlMagician(string domain, string user, string passwd)
{
try {
driver=get_driver_instance();
conn=driver->connect(domain,user,passwd);
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line "<< __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
}
SqlMagician::SqlMagician(string domain, string user, string passwd, string dbName)
{
try {
driver=get_driver_instance();
conn=driver->connect(domain,user,passwd);
useDatabase(dbName);
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line "<< __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
}
SqlMagician::~SqlMagician()
{
delete res;
delete stmt;
delete conn;
//delete driver;
}
void SqlMagician::useDatabase(string database)
{
try {
conn->setSchema(database);
stmt=conn->createStatement();
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line "<< __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
}
void SqlMagician::insertSql(string tbName, string values)
{
// cout<<"SQL COMMAND : "<<insertCmd+tbName+" values"+'('+values+");"<<endl;
try {
stmt->execute(insertCmd+tbName+" values"+'('+values+");");
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line "<< __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
cout <<"ERR CMD : "<<insertCmd+tbName+" values"+'('+values+");"<<endl;
}
}
위의 class를 이용해 간단하게 INSERT 쿼리문을 C++을 통해 전송할 수 있습니다.
아래는 main에서의 사용법 입니다.
//main.cpp
#include <iostream>
#include <SqlMagician>
int main()
{
SqlMagician sqlMagician("tcp://localhost:3306","Mysql_ID","Mysql_Passwd","Database_name");
sqlMagician.insertSql("TableName","Values");
return 0;
}
위처럼 간단하게 프로그램 내에서의 변수나 항목을 DB를 통해 관리 할 수 있습니다.
그리고 이 또한, 라이브러리 링크가 필요하며 아래와 같이 링크를 해주시면 됩니다.
# g++ -o test test.cpp -lmysqlcppconn
또는 Qt를 이용하는 분들은 아래와 같이 추가해주시면 됩니다.