-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeDB.cpp
More file actions
90 lines (75 loc) · 2.08 KB
/
Copy paththeDB.cpp
File metadata and controls
90 lines (75 loc) · 2.08 KB
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
#include "theDB.h"
#include <QMessageBox>
#include <QSqlError>
#include <QDebug>
theDB* theDB::_instance = nullptr;
theDB* theDB::instance()
{
if(_instance == nullptr) _instance = new theDB;
return _instance;
}
theDB::theDB()
{
db.setDatabaseName(DBName);
if(!db.open())
{
QMessageBox::warning(0, QString("CONNECT ERROR"), db.lastError().text());
return;
}
//指令绑定数据库
query = QSqlQuery(db);
//如果没有学生表,就创建
if(!existTable("studentTable")) createStudentTable();
//如果没有考试表,就创建
}
bool theDB::existTable(const QString& qstr)
{
if(db.tables().contains(qstr)) return true;
else return false;
}
bool theDB::createStudentTable()
{
QString sqlstr = "CREATE TABLE studentTable ("
"id INTERGER PRIMARY KEY, "
"name VARCHAR(40) NOT NULL, "
"gender VARCHAR(4) NOT NULL, "
"age INTERGER NOT NULL, "
"native VARCHAR(40),"
"photo BLOB,"
"memo MEMO);";
toQuery(sqlstr);
return true;
}
bool theDB::toQuery(const QString& qstr)
{
if(!query.exec(qstr))
{
QMessageBox::warning(0, QString("query ERROR"), query.lastError().text());
return false;
}
return true;
}
bool theDB::toQuery()
{
if(!query.exec())
{
QMessageBox::warning(0, QString("query ERROR"), query.lastError().text());
return false;
}
return true;
}
bool theDB::insertToStudentTable(const STUTABELROW& values)
{
//构造 INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
QString sqlstr("INSERT INTO studentTable "
"VALUES(:id, :name, :gender, :age, :NativePlace, :photo, :memo);");
query.prepare(sqlstr);
query.bindValue(":id", values.id);
query.bindValue(":name", values.name);
query.bindValue(":gender", values.genger);
query.bindValue(":age", values.age);
query.bindValue(":NativePlace", values.NativePlace);
query.bindValue(":photo", values.photo);
query.bindValue(":memo", values.memo);
return toQuery();
}