-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerManager.cpp
90 lines (76 loc) · 2.46 KB
/
CustomerManager.cpp
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
//
// Created by 16117 on 2022/10/27.
//
#include "CustomerManager.h"
#include "DatabaseManager.h"
#include <cstdio>
extern char query[1500];
CustomerManager::CustomerManager() {
sprintf(query,"create table if not exists customers\n"
"(\n"
"custID int auto_increment\n"
" primary key,\n"
" custName varchar(10) not null,\n"
" constraint customers_custName_uindex\n"
" unique (custName));"
"");
MYSQL* mysql = &DatabaseManager::getInstance()->mysql;
if(mysql_query(mysql, query)) {
printf("Create Customer Failed!\n%s", mysql_error(mysql));
return ;
} else {
printf("Create Customer Successs!\n");
}
}
void CustomerManager::insert() {
std::sprintf(query, "insert into customers values(NULL,'%s');", custName.c_str());
MYSQL* mysql = &DatabaseManager::getInstance()->mysql;
if(mysql_query(mysql, query)) {
printf("Insert customers Failed!(%s)\n", mysql_error(mysql));
return ;
} else {
printf("Insert customers Successs!\n");
}
}
bool CustomerManager::check() {
std::sprintf(query, "insert into customers values('%s');",custName.c_str());
MYSQL* mysql = &DatabaseManager::getInstance()->mysql;
if(mysql_query(mysql, query)) {
return false;
} else {
return true;
}
}
void CustomerManager::input() {
printf("ÇëÊäÈëÐÕÃû\n");
std::cin >> custName;
}
std::string CustomerManager::getName() {
return custName;
}
void CustomerManager::show() {
std::sprintf(query, "select * from customers");
MYSQL* mysql = &DatabaseManager::getInstance()->mysql;
if(!mysql_query(mysql, query)) {
MYSQL_RES *result = mysql_store_result(mysql);
int column = mysql_num_fields(result);
MYSQL_FIELD *field = nullptr;
int length = 12;
while((field = mysql_fetch_field(result))) {
std::cout << std::setw(length) << field->name;
}
std::cout << std::endl;
MYSQL_ROW row;
while((row = mysql_fetch_row(result))) {
for(int i = 0;i < column;i ++) {
if(row[i] == NULL) {
std::cout << std::setw(length) << "NULL";
} else {
std::cout << std::setw(length) << row[i];
}
}
std::cout << std::endl;
}
mysql_free_result(result);
}
}