Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, August 2007
(Qt 4.3,eclipse europa,mac) no database recognized


Message 1 in thread

Hello,

I would like to use a SQLITE database in a Qt program.
I entered this program:

#include <qtsql>
#include <QMessageBox>
#include <qtgui>
#include <qsqldatabase>
#include <QApplication>
#include <QString>



int main(int argc,char* argv[]){
    QApplication app(argc,argv);
    QSqlDatabase db;
    db=QSqlDatabase::addDatabase("QSQLITE");
    db.setHostName("localhost");
    db.setDatabaseName("maBase");
    db.setUserName("root");
    db.setPassword("root");
    if (!db.open()){
        QMessageBox::critical(0,"Database error",db.lastError().text());
    }
    else{
        QString res="";
        QSqlQuery query;
        query.exec("SELECT name FROM sqlite_master WHERE type='table' 
ORDER BY name");
       
        while (query.next()){
            res+=query.value(0).toString();
            res+="\n";
        }
       
       
//        for (int i=0;i<db.tables(QSql::AllTables).count();i++){
//            res+=db.tables(QSql::AllTables).at(i);
//            res+="\n";
//        }
        QMessageBox::information(0,"Database",QString("Database OK!!")+res);
    }
               
    return app.exec();
   
}

I have created the base "maBase" in SQLite manager with one table inside 
it, but when I run the program above, I obtain a window with the message 
"Database OK!!" and res is ""; the database "maBase" is not recognized, 
but why? and how to make it recognized?

lolveley.

--
 [ signature omitted ] 

Message 2 in thread

lolveley a écrit :
> Hello,
>
> I would like to use a SQLITE database in a Qt program.
> I entered this program:
>
> #include <qtsql>
> #include <QMessageBox>
> #include <qtgui>
> #include <qsqldatabase>
> #include <QApplication>
> #include <QString>
>
>
>
> int main(int argc,char* argv[]){
>    QApplication app(argc,argv);
>    QSqlDatabase db;
>    db=QSqlDatabase::addDatabase("QSQLITE");
>    db.setHostName("localhost");
>    db.setDatabaseName("maBase");
>    db.setUserName("root");
>    db.setPassword("root");
>    if (!db.open()){
>        QMessageBox::critical(0,"Database error",db.lastError().text());
>    }
>    else{
>        QString res="";
>        QSqlQuery query;
>        query.exec("SELECT name FROM sqlite_master WHERE type='table' 
> ORDER BY name");
>              while (query.next()){
>            res+=query.value(0).toString();
>            res+="\n";
>        }
>             //        for (int 
> i=0;i<db.tables(QSql::AllTables).count();i++){
> //            res+=db.tables(QSql::AllTables).at(i);
> //            res+="\n";
> //        }
>        QMessageBox::information(0,"Database",QString("Database 
> OK!!")+res);
>    }
>                  return app.exec();
>   }
>
> I have created the base "maBase" in SQLite manager with one table 
> inside it, but when I run the program above, I obtain a window with 
> the message "Database OK!!" and res is ""; the database "maBase" is 
> not recognized, but why? and how to make it recognized?
>
> lolveley.
>
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
Well, I found the solution: I changed the line containing 
setDatabaseName, I entered the whole database name, with the path and 
the extension, and IT WORKED!

lolveley.

--
 [ signature omitted ]