1 #include <QtCore/QCoreApplication> |
1 #include <QtCore/QCoreApplication> |
|
2 #include <QtSql> |
2 #include <QFile> |
3 #include <QFile> |
3 |
|
4 #include <QDebug> |
|
5 #include <QStringList> |
|
6 |
4 |
7 #include "database.h" |
5 #include "database.h" |
8 #include "dbtools.h" |
6 #include "dbtools.h" |
9 |
7 |
|
8 bool DBTools::createTable(QString sqlStmt) |
|
9 { |
|
10 qDebug() << "DB: create table statement: " << sqlStmt << endl; |
|
11 |
|
12 QSqlQuery query; |
|
13 return query.exec(sqlStmt); |
|
14 } |
|
15 |
|
16 int DBTools::getLastInsertRowId() |
|
17 { |
|
18 int rc = -1; |
|
19 QSqlQuery query; |
|
20 query.exec("select last_insert_rowid()"); |
|
21 if (!query.exec()) { |
|
22 QString errCode = "last rowid query Failed: " + query.lastError().text(); |
|
23 qWarning(errCode.toStdString().c_str()); |
|
24 |
|
25 } else { |
|
26 query.next(); |
|
27 rc = query.value(0).toInt(); |
|
28 } |
|
29 return rc; |
|
30 } |
|
31 |
|
32 void DBTools::insertContact( QString name, QString mobile, QString deskphone, |
|
33 int xtn, QString email, QString skype, |
|
34 QString twitter, QString title, int site, |
|
35 int department, int floor) |
|
36 { |
|
37 QSqlQuery query; |
|
38 |
|
39 query.prepare("INSERT INTO contacts ( name, mobile, deskphone, xtn, " |
|
40 "email, skype, twitter, title, site, department, floor) " |
|
41 "VALUES (:name,:mobile,:deskphone,:xtn,:email,:skype, " |
|
42 ":twitter, :title, :site, :department, :floor)"); |
|
43 |
|
44 query.bindValue(":name", name); |
|
45 query.bindValue(":mobile", mobile); |
|
46 query.bindValue(":deskphone", deskphone); |
|
47 query.bindValue(":xtn", xtn); |
|
48 query.bindValue(":twitter", twitter); |
|
49 query.bindValue(":skype", skype); |
|
50 query.bindValue(":email", email); |
|
51 query.bindValue(":title", title); |
|
52 query.bindValue(":site", site); |
|
53 query.bindValue(":department", department); |
|
54 query.bindValue(":floor", floor); |
|
55 |
|
56 if (!query.exec()) |
|
57 { |
|
58 qDebug() << query.lastError(); |
|
59 qFatal("Failed to add fish."); |
|
60 } |
|
61 } |
|
62 |
|
63 |
|
64 bool DBTools::createDB() |
|
65 { |
|
66 QSqlDatabase db; |
|
67 |
|
68 // Find QSLite driver |
|
69 db = QSqlDatabase::addDatabase("QSQLITE"); |
|
70 |
|
71 db.setDatabaseName("C:\\contactengine.db"); |
|
72 |
|
73 // Open databasee |
|
74 if(!db.open()) |
|
75 { |
|
76 qDebug() << "DB: failed to open" << endl; |
|
77 exit (1); |
|
78 } |
|
79 |
|
80 qDebug() << "DB: database opened " << endl; |
|
81 |
|
82 bool rc = createTable("create table contacts " |
|
83 "(cid integer primary key, " |
|
84 "name varchar(128)," |
|
85 "mobile varchar(128)," |
|
86 "deskphone varchar(128)," |
|
87 "xtn int," |
|
88 "email varchar(128)," |
|
89 "skype varchar(128)," |
|
90 "twitter varchar(128)," |
|
91 "title varchar(128)," |
|
92 "site int," |
|
93 "department int," |
|
94 "floor int)"); |
|
95 |
|
96 qDebug() << "DB: create table rc=" << rc << endl; |
|
97 return rc; |
|
98 } |
|
99 |
|
100 void DBTools::testDB() |
|
101 { |
|
102 createDB(); |
|
103 insertContact("Tom", "12345", "45678", 22, "tom@symbian.org", "thetom", "tomtom", |
|
104 "Senior Tom", SITE_LONDON, DEPT_TDM, 0); |
|
105 } |
10 |
106 |
11 void DBTools::importCSV(QString fileName) |
107 void DBTools::importCSV(QString fileName) |
12 { |
108 { |
13 QFile file(fileName); |
109 QFile file(fileName); |
14 QString line; |
110 QString line; |