-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.cpp
More file actions
95 lines (74 loc) · 2.11 KB
/
Copy pathdb.cpp
File metadata and controls
95 lines (74 loc) · 2.11 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
91
92
93
94
#include <QtCore/QDebug>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include <cassert>
#include <cstdarg>
#include "db.hpp"
using namespace std;
using namespace SMSDB;
DB::DB(const string &dbFile)
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName(dbFile.c_str());
if (!m_db.open()) {
qDebug() << m_db.lastError();
assert(false);
}
}
DB::~DB()
{
}
void DB::CreateTable(const std::string &table, const std::string &fields)
{
QSqlQuery query(("CREATE TABLE IF NOT EXISTS [" + table + "] ("
+ fields
+ ");").c_str());
query.exec();
}
void DB::DropTable(const std::string &table)
{
QSqlQuery query(("DROP TABLE IF EXISTS [" + table + "];").c_str());
query.exec();
}
void DB::Insert(const std::string &table, const std::string &fields, const int count, ...)
{
va_list args;
va_start(args, count);
string ph;
for (int i = 0; i < count; ++i) {
if (i != 0)
ph += ", ";
ph += "?";
}
QSqlQuery query(("INSERT OR IGNORE INTO [" + table + "] "
"(" + fields + ") "
"VALUES (" + ph + ");").c_str());
for(int i = 0; i < count; ++i) {
query.addBindValue(va_arg(args, char*));
}
va_end(args);
query.exec();
}
void DB::Update(const std::string &table, const std::string &where, const std::string &value,
const std::string &set, const int count, ...)
{
va_list args;
va_start(args, count);
QSqlQuery query(("UPDATE [" + table + "] "
"SET " + set + " "
"WHERE " + where + "=?;").c_str());
for(int i = 0; i < count; ++i) {
query.addBindValue(va_arg(args, char*));
}
va_end(args);
query.addBindValue(value.c_str());
query.exec();
}
void DB::Delete(const std::string &table, const std::string &where, const std::string &value)
{
QSqlQuery query(("DELETE FROM [" + table + "] "
"WHERE " + where + "=?;").c_str());
query.addBindValue(value.c_str());
query.exec();
}