Mysql API / C/C++ 연동하기 강좌 (6. mysql c++클래스 연동 예제 / 도서관리)
프로그래밍 언어/C++2009. 12. 15. 01:49
Mysql API / C/C++ 연동하기 강좌 (4. C/C++ Mysql Class 소스) 에서 작성한 클래스를 기반으로 mysql과 연동되는 간단한 도서만 입출력 및 저장수정 되는 기능을 구현해 보았습니다.
프로젝트 소스 파일 전체를 공개합니다. 또한 소스는 vs2008 버전으로 작성되었음을 밝힙니다.
테이블 정보는 아래와 같습니다.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 // CUser.h#pragma once#include "iostream"#include "stdlib.h"#include "CMysql.h"class CBook{int secNo1;int secNo2;int bookNo;char* bookName;char* writer;char* company;public:CBook(int s1, int s2, int bn, char*ba, char*wt, char*com);virtual ~CBook();int getSecNo1() { return secNo1; }int getSecNo2() { return secNo2; }int getbookNo() { return bookNo; }char* getbookName() { return bookName; }char* getwriter() { return writer; }char* getcompany() { return company; }void print();};class CBookManager{CBook *book[MAX_PATH];CMysql mysql;bool bstate;int maxRow;public:CBookManager();virtual ~CBookManager(){}void bookPrint();bool searchBook(char* bookName);bool insertBook(int secNo1, int secNo2,int bookNo, char* bookName,char* writer, char* company);bool deleteBook(char* bookName);bool AllSaveToDB();bool DBDisconnect();bool DBConnect();};// CUser.cpp#include "stdafx.h"#include "CUser.h"//////////////////////////////////////////////CBook::CBook(int s1=0, int s2=0, int bn=0,char*ba="bookName", char*wt="writer",char*com="company"){secNo1 = s1;secNo2 = s2;bookNo = bn;bookName = new char[20];writer = new char[20];company = new char[20];strcpy_s(bookName, 20, ba);strcpy_s(writer, 20, wt);strcpy_s(company, 20, com);}CBook::~CBook(){delete bookName, writer, company;}void CBook::print(){printf("| %d | %d | %d | %s | %s | %s\n", secNo1, secNo2, bookNo, bookName, writer, company);cout << "+--------------------------------------------------------------------------+" << endl;}//////////////////////////////////////////////////////CBookManager::CBookManager(){bstate = false;maxRow = 0;}bool CBookManager::DBDisconnect(){if(bstate){mysql.Disconnect();return true;}return false;}bool CBookManager::DBConnect(){if(mysql.Connect("localhost","root","******","bbs")){mysql.SelectDB("bbs");mysql.Query("select *from book");mysql.First();int i = 0;while(!mysql.IsEOF()){book[i] = new CBook(atoi(mysql["secNo1"]), atoi(mysql["secNo2"]), atoi(mysql["bookNo"]),mysql["bookName"], mysql["writer"], mysql["company"]);mysql.Next();i++;}maxRow = i;DBDisconnect();return true;}return false;}void CBookManager::bookPrint(){cout << "+--------------------------------------------------------------------------+" << endl;cout << "+ No | SecNo1 | SecNo2 | bookNo | bookName | Writer | Company +" << endl;cout << "+--------------------------------------------------------------------------+" << endl;for(int i = 0; i<maxRow; i++){book[i]->print();}}bool CBookManager::searchBook(char *bookName){for(int i = 0; i<maxRow; i++){if(strncmp(book[i]->getbookName(), bookName, strlen(bookName)) == 0){cout << "+---------------------------------------+" << endl;cout << "+ 도서번호 : " << book[i]->getbookNo() << endl;cout << "+ 도 서 명 : " << book[i]->getbookName() << endl;cout << "+ 저 자 : " << book[i]->getbookName() << endl;cout << "+ 출 판 사 : " << book[i]->getbookName() << endl;cout << "+---------------------------------------+" << endl;return true;}}cout << "+ 검색실패 : 찾으시는 도서가 없습니다" << endl << endl;return false;}bool CBookManager::insertBook(int secNo1, int secNo2, int bookNo, char* bookName, char* writer, char* company){for(int i = 0; i<maxRow; i++){if(strncmp(book[i]->getbookName(), bookName, strlen(bookName)) == 0 || book[i]->getbookNo() == bookNo){cout << "+ 중복되는 도서가 있어, 입력을 취소합니다." << endl << endl;cout << "+--------------------------------------------------------------------------+" << endl;cout << "+ No | SecNo1 | SecNo2 | bookNo | bookName | Writer | Company +" << endl;cout << "+--------------------------------------------------------------------------+" << endl;book[i]->print();return false;}}if(MAX_PATH > maxRow){book[maxRow++] = new CBook(secNo1, secNo2, bookNo, bookName, writer, company);cout << "+ 도서가 등록되었습니다" << endl << endl;return true;}else{cout << "+ 자료 입력할 공간이 부족합니다" << endl << endl;return false;}}bool CBookManager::deleteBook(char* bookName){for(int i = 0; i<maxRow; i++){if(strncmp(book[i]->getbookName(), bookName, strlen(bookName)) == 0){for(int j = i; j<maxRow-1; j++)this->book[j] = this->book[j+1];cout << "+ 자료가 삭제되었습니다" << endl << endl;maxRow--;return true;}}cout << "+ 삭제실패 : 찾으시는 도서가 없습니다" << endl << endl;return false;}bool CBookManager::AllSaveToDB(){char str[256];if(mysql.Connect("localhost","root","******","bbs")){mysql.Query("delete from book");for(int i = 0; i<maxRow; i++){sprintf(str, "insert into book value(%d,%d,%d,%d,'%s','%s','%s')",i+1,book[i]->getSecNo1(), book[i]->getSecNo2(), book[i]->getbookNo(),book[i]->getbookName(), book[i]->getwriter(), book[i]->getcompany());mysql.Query(str);}DBDisconnect();return true;;}else{cout << "+ DB연결이 되지 않습니다." << endl << endl;return false;}}//////////////////////////////////////////////////////// main.cpp// BookManager.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.//#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]){CBookManager *bm = new CBookManager();if(!bm->DBConnect()){cout << "연결 에러!" << endl;exit(1);}int a,b,c; char q[10], w[10], e[10], i;cout << "+------------------------------------+" << endl;cout << "+ C++ / MySql 클래스 제작 예제 +" << endl;cout << "+ Visual Studio 2008 +" << endl;cout << "+ MySql ver 5.1.41 +" << endl;cout << "+ by Copynull +" << endl;cout << "+------------------------------------+" << endl;while(1){cout << " 1. 도서 입력" << endl;cout << " 2. 도서 삭제" << endl;cout << " 3. 도서 검색" << endl;cout << " 4. 도서 출력" << endl;cout << " 5. 도서 저장" << endl;cout << " 6. 종료 하기" << endl;cout << "->"; i = getch(); cout << i << endl;switch(i){case '1':cout << "secNo1 : "; cin >> a;cout << "secNo2 : "; cin >> b;cout << "bookNo : "; cin >> c;cout << "bookName : "; cin >> q;cout << "writer : "; cin >> w;cout << "company : "; cin >> e;bm->insertBook(a,b,c,q,w,e);break;case '2':cout << "삭제할 도서명 입력 : "; cin >> q;bm->deleteBook(q);break;case '3':cout << "검색할 도서명 입력 : "; cin >> q;bm->searchBook(q);break;case '4':bm->bookPrint();break;case '5':if(bm->AllSaveToDB())cout << "모든 데이터 저장완료!" << endl << endl;elsecout << "저장 에러!" << endl << endl;break;case '6':cout << endl << " + Make : copynull@nate.com" << endl;cout << " + Date : 2009. 12. 15" << endl << endl;bm->DBDisconnect();exit(1);break;default:cout << "잘못 입력하셨습니다" << endl;break;}}return 0;}
프로젝트 소스 파일 전체를 공개합니다. 또한 소스는 vs2008 버전으로 작성되었음을 밝힙니다.
테이블 정보는 아래와 같습니다.
'프로그래밍 언어 > C++' 카테고리의 다른 글
1. DLL(Dynamic Linking Library) 제작 및 사용하기 (10) | 2009.12.18 |
---|---|
Mysql API / C/C++ 연동하기 강좌 (7. ODBC를 이용한 mysql 연동하기) (5) | 2009.12.17 |
Mysql API / C/C++ 연동하기 강좌 (5. mysql_field_count() 깔끔한 Error처리) (0) | 2009.12.15 |
Mysql API / C/C++ 연동하기 강좌 (4. C/C++ Mysql Class 소스) (0) | 2009.12.04 |
Mysql API / C/C++ 연동하기 강좌 (3. C/C++ Mysql 테스트 Full소스) (2) | 2009.12.02 |