-
Notifications
You must be signed in to change notification settings - Fork 2
/
user_database.cpp
163 lines (153 loc) · 4.75 KB
/
user_database.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
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
#include "class.h"
int user_insert(User user)
{
QSqlDatabase db;
if(QSqlDatabase::contains("qt_sql_default_connection"))
db = QSqlDatabase::database("qt_sql_default_connection");
else
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./data.db");
if(!db.open())
return warning("can't find data.db!", 2);
QSqlQuery query;
query.prepare
(
"insert into users " "values ( "
":id, "
":key, "
":name, "
":email, "
":phone, "
":level)"
);
query.bindValue(":id", user.id);
query.bindValue(":key", user.key);
query.bindValue(":name", user.name);
query.bindValue(":email", user.email);
query.bindValue(":phone", user.phone);
query.bindValue(":level", user.level);
if(!query.exec())
return warning("Insert fail", 1);
return warning("Insert success", 0);
}
int user_delete(QString id)
{
QSqlDatabase db;
if(QSqlDatabase::contains("qt_sql_default_connection"))
db = QSqlDatabase::database("qt_sql_default_connection");
else
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./data.db");
if(!db.open())
return warning("can't find data.db!", 2);
QSqlQuery query;
query.prepare("delete from users where id = :id");
query.bindValue(":id", id);
if (!query.exec())
return warning("Delete fail", 1);
return warning("Delete success", 0);
}
int user_modify(User user)
{
QSqlDatabase db;
if(QSqlDatabase::contains("qt_sql_default_connection"))
db = QSqlDatabase::database("qt_sql_default_connection");
else
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./data.db");
if(!db.open())
return warning("can't find data.db!", 2);
QSqlQuery query;
query.prepare
(
"update users set "
"key = :key, "
"name = :name, "
"email = :email, "
"phone = :phone, "
"level = :level "
"where id = :id"
);
query.bindValue(":id", user.id);
query.bindValue(":key", user.key);
query.bindValue(":name", user.name);
query.bindValue(":email", user.email);
query.bindValue(":phone", user.phone);
query.bindValue(":level", user.level);
if (!query.exec())
return warning("Modify fail", 1);
return warning("Modify success", 0);
}
int search_user_id(User &user, QString user_id)
{
if(user_id == "")
return warning("Please input id", 1);
QSqlDatabase db;
if(QSqlDatabase::contains("qt_sql_default_connection"))
db = QSqlDatabase::database("qt_sql_default_connection");
else
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./data.db");
if(!db.open())
return warning("can't find data.db!", 2);
enum type{id, key, name, email, phone, level};
QSqlQuery query;
query.prepare("select * from users where id = :id");
query.bindValue(":id", user_id);
if(!query.exec())
return warning("Search id fail", 1);
while(query.next())
{
user.id = query.value(id).toString();
user.key = query.value(key).toString();
user.name = query.value(name).toString();
user.email = query.value(email).toString();
user.phone = query.value(phone).toString();
user.level = query.value(level).toInt();
}
if(user.id != user_id)
return warning("Search id fail", 1);
return warning("Success!", 0);
}
int user_search(QVector<User> &user, QString str, int flag)
{
QSqlDatabase db;
if(QSqlDatabase::contains("qt_sql_default_connection"))
db = QSqlDatabase::database("qt_sql_default_connection");
else
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./data.db");
if(!db.open())
return warning("Can't find data.db!", 2);
User tem;
user.clear();
enum type{id, key, name, email, phone, level};
for(int i = str.size() - 1; i >= 0; i--)
if(str[i] == QChar(' '))
str[i] = QChar('%');
str = "%" + str + "%";
QSqlQuery query;
if(flag == id)
{
query.prepare("select * from users where id like :id");
query.bindValue(":id", str);
}
if(flag == name)
{
query.prepare("select * from users where name like :name");
query.bindValue(":name", str);
}
if(!query.exec())
return warning("Search fail", 1);
while (query.next())
{
tem.id = query.value(id).toString();
tem.key = query.value(key).toString();
tem.name = query.value(name).toString();
tem.email = query.value(email).toString();
tem.phone = query.value(phone).toString();
tem.level = query.value(level).toInt();
user.push_back(tem);
}
return warning("Success", 0);
}