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