Thinking Different




Mysql API / C/C++ 연동하기 강좌 (4. C/C++ Mysql Class 소스) 에서 작성한 클래스를 기반으로 mysql과 연동되는 간단한 도서만 입출력 및 저장수정 되는 기능을 구현해 보았습니다.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// 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;
            else
                cout << "저장 에러!" << 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 버전으로 작성되었음을 밝힙니다.
테이블 정보는 아래와 같습니다.


BookManager.zip