#include #include void createTable(QSqlDatabase *pDb) { if (pDb) { QSqlQuery createTable("CREATE TABLE TESTTABLE (ID NUMBER(10) NOT NULL, EventDate DATE)"); if (createTable.exec()) qDebug() << "Successfully created table."; else qDebug() << "Failed to create table."; } } void insertRecords(QSqlDatabase *pDb, const QDateTime &dt) { if (pDb) { for (int i = 0; i < 5; i++) { QSqlQuery insertQuery; insertQuery.prepare("INSERT INTO TESTTABLE VALUES (?, ?)"); insertQuery.bindValue(0, i); insertQuery.bindValue(1, dt); if (insertQuery.exec()) qDebug() << "Successfully inserted record."; else qDebug() << "Failed to insert record."; } } } void findRecords(QSqlDatabase *pDb, const QDateTime &startDate, const QDateTime &endDate) { QSqlQuery query; query.prepare("SELECT * FROM TESTTABLE WHERE EventDate >= ? AND EventDate <= ?"); query.addBindValue(startDate); query.addBindValue(endDate); if (query.exec()) { int count = 0; while (query.next()) { qDebug() << "Found record: " << query.value(0).toString(); count++; } if (count == 0) qDebug() << "No results found."; } } int main(int argc, char ** argv) { QSqlDatabase db = QSqlDatabase::addDatabase("QOCI"); db.setHostName(""); // change to oracle 10g host db.setPort(1521); // port db.setDatabaseName(""); // SID db.setUserName(""); db.setPassword(""); if (db.open()) { QDateTime currentDate = QDateTime::currentDateTime(); QDateTime endDate = currentDate.addDays(1); createTable(&db); insertRecords(&db, currentDate.toUTC()); // 1st attempt, query for records in between the two date times. Will return 0 records. QDateTime startDate = currentDate.addSecs(-60); // query start date 1 minute before current time findRecords(&db, startDate.toUTC(), endDate.toUTC()); // 2nd attempt, query for records in between the two date times but set start date 1 hour back. Will return 5 records. QDateTime startDate2 = startDate.addSecs(-3600); // query start date 1 hour before original start date findRecords(&db, startDate2.toUTC(), endDate.toUTC()); } return 0; }