[Qt-interest] A Question about Sqlite in Qt

Kermit Mei kermit.mei at gmail.com
Thu Jun 11 04:25:32 CEST 2009


Hello,all, I firstly use QSQLite with Qt4, and I don't know how to get a
sql sentence's result. My code is like this :

Firstly, in Linux command Line I use mytestdb like this:

$ sqlite3 mytestdb 
SQLite version 3.6.10
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from person where id='1010' or id='10000';
1010|Danny|Young
10000|Danny|Young

The result is OK. But in Qt I can't get it. This is my Qt code, please
help me to know what's wrong with my program:

#include <QApplication>

#include "connection.h"
#include <iostream>
#include <ctime>

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
                                                                                  
  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  db.setDatabaseName("./mytestdb");
  if (!db.open()) {
    std::cerr << "Unable to establish a database connection.\n";
    return 1;
  }else
    std::cerr << "Establish a database connection.\n";

  QSqlQuery query;
  int i = time(0);
  query.exec("select * from person where id='1010' or id='10000'");
  i = time(0) - i;
  std::cerr << "Time you used is: " << i << " seconds to get it!" <<
std::endl;
  std::cerr << query.isValid() << std::endl;
  return app.exec();
}

compile and run it, the result is :
$ ./test 
Establish a database connection.
Time you used is: 0 seconds to get it!
0

How can I get the result like sqlite3 did?

Thank you!





More information about the Qt-interest-old mailing list