-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
85 lines (74 loc) · 2.49 KB
/
main.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
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QDir>
#include <QMessageBox>
#include <QFile>
#include <QTextCodec>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Set text codec
QTextCodec::setCodecForCStrings( QTextCodec::codecForName( "UTF-8" ) );
QTextCodec::setCodecForTr( QTextCodec::codecForName( "UTF-8" ) );
// Detect user home path
QString sep = QDir::separator();
QString dbPath = QDir::homePath() + sep + "urDiary.db";
dbPath = QDir::toNativeSeparators(dbPath);
// Add library path
a.addLibraryPath(a.applicationDirPath());
a.addLibraryPath(a.applicationDirPath() + sep + "sqldrivers");
// Find DB and build new DB if not found
QFile file(dbPath);
bool dbExist = true;
if( !file.exists() ){
dbExist = false;
}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName( dbPath );
if( !db.open() ){
QMessageBox::critical(0,"Can not open database"
,"Unable to establish a database connection. \n"
"Path: " + dbPath + "\n"
"Error: " + db.lastError().text()
,QMessageBox::Ok);
return 1;
}
QSqlQuery query(db);
// DB initialize
if( dbExist == false ){
query.exec("create table diary ("
"id integer primary key AUTOINCREMENT,"
"date text, "
"field1 text, "
"field2 text, "
"field3 text, "
"field4 text, "
"field5 text, "
"field6 text, "
"field7 text, "
"field8 text, "
"anniversary text, "
"birth text, "
"fate text, "
"meet text, "
"specialDate text, "
"weather text, "
"getup text )");
query.exec("create table sys ( "
"id integer primary key AUTOINCREMENT, "
"var text, "
"val text )");
bool result = query.exec("insert into sys (var, val) values('dbversion','1')");
result = query.exec("insert into sys (var, val) values('start','"
+ QDate::currentDate().toString("yyyyMMdd") +
"')");
}
MainWindow w;
w.setDb(&db, &query);
w.initForm();
w.show();
return a.exec();
}