양다방

    MariaDB OpenFrameWorks 연동

    MariaDB는 최근 버전은 모두 64bit로 제공되고 있어서 OpenFrameWorks 0.9.8을 기반으로 32bit로 주로 개발을 하는 내 입장에서는 불편할 수 밖에 없다. 게다가 Compute Shader를 많이 사용하는 내 개발에서는 부득불 모든 코드를 0.12.1로 업그레이드해야 하는 이 상황이 마뜩지 않다. 정말 매우 매우 불편하지만 그래도 할 건 해야지.

    일단 MariaDB를 설치하고 설치한 후에는 lib와 include가 Program Files/MariaDB… / 안에 다 들어가 있으니 그걸 내 프로젝트에 연결해준다. 당연히 dll은 내 프로젝트 바이너리 폴더에 옮겨준다. 아래 그림 4개 참고.

    MariaDB용 DB도 만들어주고, 사용자도 만들어주고, 테스트용 테이블도 하나 만들어준다.

    >> DB만들고 사용자 만드는 부분.
    >> 여기부터는 mysql cli 콘솔 들어가서 넣어주면 된다.
    create database llmactivity default character set utf8 collate utf8_general_ci;
    create user 'llm'@'localhost' identified by '43348098';
    create user 'llm'@'%' identified by '43348098';
    grant all privileges on llmactivity.* to 'llm'@'localhost';
    grant all privileges on llmactivity.* to 'llm'@'%';
    flush privileges;
    
    >> DB 테이블 만드는 부분.  
    CREATE TABLE talktalk (
    	idx int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
    	question varchar(250) NOT NULL,
    	answer text,
    	language varchar(15),
    	filename text,
    	voice varchar(50),
    	date int(10) unsigned NOT NULL DEFAULT '0'
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

    ofApp.h에 위에 include 하나 추가해주고, MySQL 포인터 하나 만들어준다.

    #pragma once
    
    #include <mysql.h>
    
    #include "ofMain.h"
    
    class ofApp : public ofBaseApp{
    
    	public:
    		void setup();
    		void update();
    		void draw();
    
    		void keyPressed(int key);
    		void keyReleased(int key);
    		void mouseMoved(int x, int y );
    		void mouseDragged(int x, int y, int button);
    		void mousePressed(int x, int y, int button);
    		void mouseReleased(int x, int y, int button);
    		void mouseEntered(int x, int y);
    		void mouseExited(int x, int y);
    		void windowResized(int w, int h);
    		void dragEvent(ofDragInfo dragInfo);
    		void gotMessage(ofMessage msg);
    
    		MYSQL* con;
    
    
    		auto execSQLQuery(MYSQL* connection, string query)
    		{
    			struct result
    			{
    				bool success;
    				MYSQL_RES* res;
    			};
    
    			bool success = true;
    			if (mysql_query(connection, query.c_str()))
    			{
    				cout << "MySQL Query Error : " << mysql_error(connection) << endl;
    				success = false;
    			}
    			return result
    			{
    				success,
    				mysql_use_result(connection)
    			};
    		}
    
    };
    

    execSQLQuery 함수는 잘 짜놓은 코드가 있어 이 곳에서 가져다 추가했다.

    https://ajdxjdrnfld.tistory.com/12

    ofApp.cpp 에서 setup함수에서만 아래 테스트를 실행해본다.

    void ofApp::setup(){
    	con = mysql_init(NULL);
    	if (!mysql_real_connect(con, "localhost", "사용자명", "비번", "DB명", 0, NULL, 0))
    	{
    		cout << " Connection Error : " << mysql_error(con) << endl;
    	}
    	else {
    		cout << "MariaDB Connection Success " << endl;
    	}
    
    
    	MYSQL_ROW row;
    	if (con->status == mysql_status::MYSQL_STATUS_READY) {
    
    		auto result = execSQLQuery(con, "insert into talktalk (question, answer) values ('hello3', 'world3');");
    
    		if (!result.success)
    		{
    			cout << "fail to input to talktalk" << endl;
    		}
    
    	}
    
    	if (con->status == mysql_status::MYSQL_STATUS_READY) {
    
    
    		auto result = execSQLQuery(con, "select * from talktalk;");
    
    		if (!result.success)
    		{
    			cout << "fail to access talktalk" << endl;
    		}
    
    		cout << "Database Output : \n" << endl;
    
    		while ((row = mysql_fetch_row(result.res)) != NULL)
    		{
    			cout << "result fields num : " << mysql_num_fields(result.res) << endl;
    			cout << "result row num : " << mysql_num_rows(result.res) << endl;
    
    			cout << row[0] << " | " << row[1] << endl;
    		}
    
    	}
    
    
    	if (con->status == mysql_status::MYSQL_STATUS_READY) {
    
    		auto result = execSQLQuery(con, "update talktalk set question='minha1', answer='babo1' where idx='1';");
    
    		if (!result.success)
    		{
    			cout << "fail to update to talktalk" << endl;
    		}
    
    	}
    
    
    	if (con->status == mysql_status::MYSQL_STATUS_READY) {
    
    
    		auto result = execSQLQuery(con, "select * from talktalk;");
    
    		if (!result.success)
    		{
    			cout << "fail to access talktalk" << endl;
    		}
    
    		cout << "Database Output v2 : \n" << endl;
    
    		while ((row = mysql_fetch_row(result.res)) != NULL)
    		{
    			cout << "result fields num : " << mysql_num_fields(result.res) << endl;
    			cout << "result row num : " << mysql_num_rows(result.res) << endl;
    
    			cout << row[0] << " | " << row[1] << endl;
    		}
    
    	}
    
    
    	if (con->status == mysql_status::MYSQL_STATUS_READY) {
    
    		auto result = execSQLQuery(con, "delete from talktalk where answer='babo1';");
    
    		if (!result.success)
    		{
    			cout << "fail to update to talktalk" << endl;
    		}
    
    	}
    
    	if (con->status == mysql_status::MYSQL_STATUS_READY) {
    
    
    		auto result = execSQLQuery(con, "select * from talktalk;");
    
    		if (!result.success)
    		{
    			cout << "fail to access talktalk" << endl;
    		}
    
    		cout << "Database Output v3 : \n" << endl;
    
    		while ((row = mysql_fetch_row(result.res)) != NULL)
    		{
    			cout << "result fields num : " << mysql_num_fields(result.res) << endl;
    			cout << "result row num : " << mysql_num_rows(result.res) << endl;
    
    			cout << row[0] << " | " << row[1] << endl;
    		}
    
    	}
    
    }

    잘된다 당연히.


    게시됨

    카테고리

    작성자

    태그: