author | John Kern <johnk@symbian.org> |
Fri, 10 Sep 2010 09:00:20 -0700 | |
changeset 48 | 863c77d15828 |
parent 43 | 105a16347b5a |
permissions | -rw-r--r-- |
20
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
1 |
#include <QtCore/QCoreApplication> |
43 | 2 |
#include <QtSql> |
21
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
3 |
#include <QFile> |
20
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
4 |
|
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
5 |
#include "database.h" |
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
6 |
#include "dbtools.h" |
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
7 |
|
43 | 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 |
} |
|
20
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
106 |
|
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
107 |
void DBTools::importCSV(QString fileName) |
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
108 |
{ |
21
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
109 |
QFile file(fileName); |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
110 |
QString line; |
20
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
111 |
|
21
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
112 |
if (file.open(QFile::ReadOnly)) { |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
113 |
qDebug() << "importCSV file open OK" << endl; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
114 |
char buf[1024]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
115 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
116 |
while(!file.atEnd()) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
117 |
{ |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
118 |
file.readLine(buf, sizeof(buf)); |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
119 |
QString string(buf); |
24
2e833c2a6782
Added csv file to sis exports; improved csv file location in main.cpp
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
21
diff
changeset
|
120 |
|
21
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
121 |
QStringList stringList = string.split(","); |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
122 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
123 |
QString strName; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
124 |
QString strMobile; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
125 |
QString strDeskphone; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
126 |
QString strXtn; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
127 |
QString strEmail; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
128 |
QString strFloor; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
129 |
QString strSkype; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
130 |
QString strTwitter; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
131 |
QString strTitle; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
132 |
QString strSite; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
133 |
QString strDepartment; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
134 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
135 |
if (stringList.count() > 0) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
136 |
strName = stringList[0]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
137 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
138 |
if (stringList.count() > 1) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
139 |
strMobile = stringList[1]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
140 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
141 |
if (stringList.count() > 2) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
142 |
strDeskphone = stringList[2]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
143 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
144 |
if (stringList.count() > 3) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
145 |
strXtn = stringList[3]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
146 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
147 |
if (stringList.count() > 4) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
148 |
strEmail = stringList[4]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
149 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
150 |
if (stringList.count() > 5) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
151 |
strSkype = stringList[5]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
152 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
153 |
if (stringList.count() > 6) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
154 |
strTwitter = stringList[6]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
155 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
156 |
if (stringList.count() > 7) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
157 |
strTitle = stringList[7]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
158 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
159 |
if (stringList.count() > 8) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
160 |
strSite = stringList[8]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
161 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
162 |
if (stringList.count() > 9) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
163 |
strDepartment = stringList[9]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
164 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
165 |
if (stringList.count() > 10) |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
166 |
strFloor =stringList[10]; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
167 |
|
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
168 |
qDebug() << strName << strTitle << endl; |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
169 |
} |
3bfc3227045d
CSV reader works, kind of. contacts.csv file added to project, must live in c:\ at the moment
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
20
diff
changeset
|
170 |
} |
20
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
171 |
} |
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
172 |
|
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
173 |
DBTools::DBTools(QObject *parent) |
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
174 |
{ |
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
175 |
QObject(); |
43 | 176 |
createDB(); |
20
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
177 |
} |
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
178 |
|
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
179 |
DBTools::~DBTools() |
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
180 |
{ |
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
181 |
|
a7451a8eb5dc
Added DB framework for CSV import - doesn't work yet though
Sebastian Brannstrom <sebastianb@symbian.org>
parents:
diff
changeset
|
182 |
} |