-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyUtil.cpp
More file actions
71 lines (61 loc) · 1.7 KB
/
MyUtil.cpp
File metadata and controls
71 lines (61 loc) · 1.7 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
#include "MyUtil.h"
namespace UTILS
{
QString FileUtil::ReadFileToQString(const char *path)
{
QFile f(path);
if (!f.exists() | !f.open(QIODevice::ReadOnly | QIODevice::Text)) {//打开指定文件
qDebug() << "打开失败";
return nullptr;
}
QTextStream in(&f);
in.setCodec("UTF-8");
return in.readAll();
}
bool SqliteUtil::CreateTablesBySQL(QString qs, QSqlQuery *qq)
{
QStringList qlist = qs.split(";");
for (int i = 0; i < qlist.size() - 1; i++) {
QString qs1 = qlist.at(i).toUtf8();
try {
qq->exec(qs1);
} catch (...) {
qDebug() << "创建失败" << qs1;
}
}
return true;
}
// 占位符:imgData
bool SqliteUtil::InsertDataWithBinaryBySQL(QString qs, QSqlQuery *qq)
{
QStringList qlist = qs.split(";");
QString qs2;
qDebug() << "size:" << qlist.size();
for (int i = 0; i < qlist.size() - 1; i++) {
QString qs1 = qlist.at(i).toUtf8();
try {
QFile file(qs2.asprintf(":/static/food/%d.jpg", i + 1));
if (!file.open(QIODevice::ReadOnly)) return false;
QByteArray byte = file.readAll();
qq->prepare(qs1);
qq->bindValue(":imgData", byte);
qq->exec();
} catch (...) {
qDebug() << "创建失败" << qs1;
return false;
}
}
return true;
}
void SqliteUtil::SelectAndShowByRange(QString tablename, QSqlQuery *qq, int range)
{
QString qs("select * from " + tablename + ";");
qq->exec(qs);
while (qq->next()) {
QString tqs;
for (int i = 0; i < range; i++)
tqs = tqs.append(qq->value(i).toString() + ";");
qDebug() << tqs;
}
}
}