| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 불가피
- 스콜피온킹
- 김영권 아내
- 이상형 만들기
- 리리남매
- 스페인 코로나
- 양적완화
- 이지혜
- 홍혜걸
- 유튜버 김재석
- 김재석
- 김영권
- 은혜의 강 교회
- 금리인하
- 픽크루
- 이태원 클라쓰 15회 예고
- 제넥신
- 미국 금리인하
- 폰폰테스트
- 조희연
- 성남 코로나 확진자
- 고민정
- 최강욱
- 학교 개학 연기 4월
- 임영규
- 뭉쳐야 찬다
- 킹덤 고근희
- 성남은혜의강교회
- libtins
- 해킹
Archives
- Today
- Total
Dork's port
[C++] Kali Linux에서 Mysql 연동해서 사용하기. 본문
안녕하세요.
오늘은 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를 이용하는 분들은 아래와 같이 추가해주시면 됩니다.
'Develop' 카테고리의 다른 글
| Python 문자열 앞의 공백 지우기 (0) | 2018.03.16 |
|---|---|
| Pycharm indexing 무한 반복(indexing infinite loop) 해결 방법 (0) | 2018.03.16 |
| [C++] LibTins를 이용한 AP Scanner (Beacon Sniffer,airodump-ng) (0) | 2017.12.13 |
| [C++] thread invalid use of non-static member function error (0) | 2017.12.09 |
| [C++] LibTins를 통한 802.11 Packet Sniff (1) | 2017.12.01 |
Comments