-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfreeQueryThread.cpp
More file actions
46 lines (34 loc) · 1.29 KB
/
freeQueryThread.cpp
File metadata and controls
46 lines (34 loc) · 1.29 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
#include "freeQueryThread.h"
#include <QSqlQueryModel>
void freeQueryThread::run() {
QString querystr=this->window->queryedit->text().simplified();
//qDebug()<<querystr;
if(!querystr.startsWith("SELECT",Qt::CaseInsensitive)) {emit fail(this,READONLY_ERROR);return;}
QSqlDatabase db=window->db;
QSqlQuery query(db);
db.open();
if(!query.exec(querystr)) {emit fail(this,SQL_EXEC_ERROR);return;}
QSqlQueryModel model;
model.setQuery(query);
int num_column=model.columnCount();
for(int i=0;i<num_column;i++) window->tableheader.push_back("");
for(int i=0;i<window->fields_onehot.size();i++) {
auto index=query.record().indexOf(window->import_fields[i]);
if (index>=0 && index<num_column)
window->tableheader[index]=window->import_fields[i];
}
qDebug()<<query.lastError();
int cnt=0;
while(query.next()){
cnt++;
vector<QString> l;
for(int i=0;i<num_column;i++) l.push_back(QString(query.value(i).toString()));
window->records.push_back(l);
}
//qDebug()<<cnt;
window->datatable->setColumnCount(num_column);
window->datatable->setRowCount(window->records.size()+1);
window->datatable->setHorizontalHeaderLabels(window->tableheader);
emit success(this);
quit();
}