-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdatafromlocal.cpp
133 lines (125 loc) · 4.39 KB
/
datafromlocal.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
#include "datafromlocal.h"
#include "ui_datafromlocal.h"
DataFromLocal::DataFromLocal(QWidget *parent) :
QDialog(parent),
ui(new Ui::DataFromLocal)
{
ui->setupUi(this);
setStyleSheet("font:11pt ;background-color: rgb( 90, 90, 90);color: white;"); //字体大小颜色
// mydatabase = new MyDataBase("localhost","Data/test1.db");
mydatabase = nullptr;
// 数据库名只能输入字母与数字
QRegExp regx("[a-zA-Z0-9]+$");
QValidator *validator = new QRegExpValidator(regx, ui->databaseName );
ui->databaseName->setValidator( validator );
}
DataFromLocal::~DataFromLocal()
{
delete ui;
if(nullptr != mydatabase)
{
delete mydatabase;
}
}
// 选择文件
void DataFromLocal::on_choseFile_clicked()
{
QFileDialog *fileDialog=new QFileDialog();
fileDialog->setFileMode(QFileDialog::ExistingFile);
fileDialog->setNameFilter(("file(*.csv *.txt)")); //*.xls *.xlsx
QString openPath=QCoreApplication::applicationDirPath();
fileDialog->setDirectory(openPath);
if(fileDialog->exec()==QDialog::Accepted)
{
QString srcFile=fileDialog->selectedFiles()[0];
ui->lineEdit->setText(srcFile);
}
}
// 将选中文件导入数据库
void DataFromLocal::on_toDatabase_clicked()
{
// 创建mydatabase实例
if(nullptr != mydatabase)
{
delete mydatabase;
}
QString databaseName = ui->databaseName->text();
if(databaseName.isEmpty())
{
QMessageBox::warning(this, QString::fromLocal8Bit("导入异常"),QString::fromLocal8Bit("数据库名为空,请确认!"),QMessageBox::Ok);
return;
}
QString databasePath = QString("Data/") +databaseName + QString(".db");
mydatabase = new MyDataBase("localhost",databasePath);
//获得csv表格名称
QString srcPath=ui->lineEdit->text();
if(srcPath.isEmpty())
{
QMessageBox::warning(this, QString::fromLocal8Bit("导入异常"),QString::fromLocal8Bit("路径为空,请确认!"),QMessageBox::Ok);
return;
}
QStringList fileList;
fileList.clear();
fileList = srcPath.split("/"); //路径按"/"切分
QString filename = fileList.at(fileList.size()-1).split(".").at(0) ;
//判断表格是否存在
if(mydatabase->IsExist(filename))
{
qDebug()<<QString::fromLocal8Bit("同名表格已经存在");
return;
}
// 读取数据文件
QFile file(srcPath);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
// 文件打开,一行行写入数据库
QStringList firstList; //第一行
firstList.clear();
QTextStream in(&file);
// 第一行
QString fileLine = in.readLine();
firstList = fileLine.split(","); //csv格式以分号间隔
//qDebug()<< dataList.size();
data_name.clear();
data_name.insert("id", "int primary key"); //data_name.insert("id", "int primary key autoincrement");
for(int i=0;i<firstList.size();i++)
{
data_name.insert(firstList.at(i),"varchar"); //不同的表格是不同的数据格式
}
//创建表
mydatabase->create_table(filename,data_name);
// 开始启动事务
mydatabase->m_db.transaction();
//插入记录
QTime t;
t.start();
int lineNumber = 0;
while(!in.atEnd())
{
lineNumber++;
data_name.clear();
fileLine = in.readLine();
QStringList dataList;
dataList.clear();
dataList = fileLine.split(",");
for(int i=0;i<firstList.size();i++)
{
data_name.insert("id",QString::number(lineNumber));
data_name.insert(firstList.at(i),dataList.at(i)); //不同的表格是不同的数据格式
}
mydatabase->add(filename,data_name);
}
// 提交事务
mydatabase->m_db.commit();
qDebug()<<QObject::tr("insert %1 pieces of data into database spend: %2 ms").arg(lineNumber).arg(t.elapsed());
// 关闭文件
file.close();
}
else
{
QMessageBox::warning(this, QString::fromLocal8Bit("导入异常"),QString::fromLocal8Bit("文件打开错误,请确认!"),QMessageBox::Ok);
qDebug()<< "Cannot open file for reading"
<< qPrintable(file.errorString());
}
qDebug()<<QString::fromLocal8Bit("数据导入完成");
}