[Qt-interest] A Question about Sqlite in Qt

Kermit Mei kermit.mei at gmail.com
Thu Jun 11 06:58:31 CEST 2009


On Wed, 2009-06-10 at 21:25 -0700, Scott Aron Bloom wrote:
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com
> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Kermit Mei
> Sent: Wednesday, June 10, 2009 7:26 PM
> To: qt-interest at trolltech.com
> Subject: [Qt-interest] A Question about Sqlite in Qt
> 
> 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();
> }
> -----------------
> If the exec returns true, then do the following:
> While( query.next() ) // moves the cursor to the first record
> {
>     QVaraint first = query.value( 0 );
>     QVaraint third = query.value( 1 );   
>     QVaraint second = query.value(  );
> }
> 
> Look at the methods on QSqlQuery
> 
> Scott

Oh,yes I see, thanks you all:P





More information about the Qt-interest-old mailing list