src/examples/occi_datatypes.cpp

Go to the documentation of this file.
00001 #include <QApplication>
00002 #include <QtSql>
00003 
00004 #include <iostream>
00005 using namespace std;
00006 
00007 /* Change these to match your database */
00008 #define HOST   "127.0.0.1"
00009 #define DB     "xe"
00010 #define PORT   1521
00011 #define USER   "testbed"
00012 #define PASSWD "secret"
00013 
00014 const char create_sql[] = 
00015     "create table occi_datatype_example_tb ( \
00016         name       varchar(25), \
00017         age        number, \
00018         letter     char(2), \
00019         some_date  date, \
00020         some_time  timestamp, \
00021         bin_data   blob)";
00022  
00023 // standard insert
00024 const char insert_sql_1[] = 
00025     "insert into occi_datatype_example_tb \
00026     (name, age, letter, some_date, some_time) \
00027     values \
00028     ('foobar', 25, 'jg', sysdate, sysdate)";
00029 
00030 // use bindings...
00031 const char insert_sql_2[] = 
00032     "insert into occi_datatype_example_tb \
00033     (name, age, letter, some_date, some_time, bin_data) \
00034     values \
00035     (:1, :2, :3, :4, :5, :6)";
00036 
00037 const char select_sql[] = 
00038     "select name, age, letter, some_date, some_time, bin_data \
00039     from occi_datatype_example_tb \
00040     where name = 'joe'";
00041     
00042 const char random_data[] = {43, 242, 13, 01, 00, 123, 2, 4};
00043 
00044 int main(int argc, char *argv[])
00045 {
00046     QApplication app(argc, argv);
00047  
00048     QSqlDatabase qDB = QSqlDatabase::addDatabase("QOpenOCCI");
00049     
00050     qDB.setHostName(HOST);
00051     qDB.setDatabaseName(DB);
00052     qDB.setPort(PORT);
00053     qDB.setUserName(USER);
00054     qDB.setPassword(PASSWD);
00055     
00056     /* Probably overkill ;) */
00057     qDB.setConnectOptions("Prefetch_Memory=5242880;Prefetch_Rows=250");
00058     
00059     if (qDB.open() == FALSE) {
00060         cerr << "Error: opening DB failed because " << qDB.lastError().text().toStdString() << endl;
00061         return -1;
00062     }
00063 
00064     QSqlQuery query(qDB);
00065     
00066     // drop the old table and we don't care if it fails..
00067     query.exec("drop table occi_datatype_example_tb");
00068     
00069     if (query.exec(create_sql) == FALSE) {
00070         cerr << "Error: query failed because " << query.lastError().text().toStdString() << endl;
00071         return -1;
00072     }
00073     
00074     if (query.exec(insert_sql_1) == FALSE) {
00075         cerr << "Error: query failed because " << query.lastError().text().toStdString() << endl;
00076         return -1;
00077     }
00078     
00079     if (query.prepare(insert_sql_2) == FALSE) {
00080         cerr << "Error: prepare failed because " << query.lastError().text().toStdString() << endl;
00081         return -1;
00082     }
00083     
00084     query.addBindValue(QString("joe"));
00085     query.addBindValue(13);
00086     query.addBindValue(QString("bo"));
00087     query.addBindValue(QDate(2008, 02, 16));
00088     query.addBindValue(QDateTime(QDate(2008, 01, 01), QTime(12, 00, 00)));
00089     query.addBindValue(QByteArray(random_data, 8));
00090     
00091     if (query.exec() == FALSE) {
00092         cerr << "Error: exec failed because " << query.lastError().text().toStdString() << endl;
00093         return -1;
00094     }
00095     
00096     if (query.exec(select_sql) == FALSE) {
00097         cerr << "Error: query failed because " << query.lastError().text().toStdString() << endl;
00098         return -1;
00099     }    
00100     
00101     while (query.next()) {
00102         cout << "Name: " << query.value(0).toString().toStdString() << endl;
00103         cout << "Age: " << query.value(1).toInt() << endl;
00104         cout << "Code: " << query.value(2).toString().toStdString() << endl;
00105         cout << "Date: " << query.value(3).toDate().toString(Qt::TextDate).toStdString() << endl;
00106         cout << "Time: " << query.value(4).toDateTime().toString(Qt::TextDate).toStdString() << endl;
00107         // cast the data as two ints and print out their value in hex 
00108         cout << "Data: " << hex <<  ((int*)(query.value(5).toByteArray().data()))[0] << ((int*)(query.value(5).toByteArray().data()))[1] << endl;
00109     }
00110     return 0;
00111 }

Generated on Tue Mar 18 22:47:08 2008 for QOpenOCCI by  doxygen 1.5.3