×

Discussion Board

Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    17

    Add existing Sqlite database to Qt project

    Hello,

    I want to include in my Qt project one SQLite database which I have created previously, and I have a problem, when I test my application, it creates a new database instead of open the existing one. My database is located on the root directory of my project, and this is my code to open it:


    bool FavoritosInicial:penDB()
    {
    // Cargando QSLite driver
    db = QSqlDatabase::addDatabase("QSQLITE");

    db.setDatabaseName("mydb");

    return db.open();
    }

    I've tried db.setDatabaseName(":mydb"); too

    Then, I try to read data from this database:

    QSqlQuery query;
    query.exec("select nombre, tipo from t_favoritos");

    while (query.next()) {
    QString nombre = query.value(0).toString();
    QString tipo = query.value(1).toString();
    qDebug() << "Nombre:" << nombre << "; Tipo: " << tipo;
    }

    But I don't get any data...
    Please, I appreciate any help.

    Thanks in advance.
    Regards.

  2. #2
    Super Contributor
    Join Date
    Oct 2008
    Location
    INDIA
    Posts
    2,326

    Re: Add existing Sqlite database to Qt project

    Search in the wiki DIBO,

    Check this, http://discussion.forum.nokia.com/fo...g-SQL-database

    Best regards,

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    17

    Re: Add existing Sqlite database to Qt project

    Thanks Rahulvala, I have read this thread, and after some changes in my code I have discovered that my database is not included in my project when I generate the installable package, I mean, my database is not being installed with the project.

    If you know, could you say me how can I include the database file in my project, please?

    Thanks in advance.
    Regards.

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Add existing Sqlite database to Qt project

    Test application:
    Code:
    #include <QtCore>
    #include <QtSql>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(":mydb");
        if (!db.open())
            qDebug() << db.lastError();
        else {
            qDebug() << db.tables();
            db.close();
        }
    }
    However I don't think you can use database from Qt resources. How would you be saving data to the database, then?

  5. #5
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Add existing Sqlite database to Qt project

    Daniil, there is a bug (or maybe "feature") in SQLite (on Symbian at least) in that it requires the full path to be specified if the app may be loaded anywhere other than the default directory of the default drive. My post pointed to by rahulvala's post provides a "cookbook" solution to this problem.

  6. #6
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Add existing Sqlite database to Qt project

    You need to "deploy" the database. This involves some hocus-pocus in the PRO file. If you have the file in the root of your Qt project and want to deploy it on the device into the default private directory of your application you would use the following:
    Code:
        database.sources = "MyDb.db"
        database.path = .
        DEPLOYMENT += database

  7. #7
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: Add existing Sqlite database to Qt project

    Quote Originally Posted by danhicksbyron View Post
    Daniil, there is a bug (or maybe "feature") in SQLite (on Symbian at least) in that it requires the full path to be specified if the app may be loaded anywhere other than the default directory of the default drive. My post pointed to by rahulvala's post provides a "cookbook" solution to this problem.
    On Linux there is no problem with relative paths.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    17

    Re: Add existing Sqlite database to Qt project

    Thanks, I included those changes in my PRO file, and my database was "installed" too.

    Thanks a lot.
    Best Regards.

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    11

    Re: Add existing Sqlite database to Qt project

    HI,

    I am facing the same problem mentioned above " new batabase is getting created instead of opening the existing one". database is placed in the project folder.
    Here is the code snippet.

    pro file

    QT += core gui \
    sql

    database.sources = E:/nTRak/mTrak.db
    database.path = Private/eeb72120/mTrak.db
    DEPLOYMENT+= database

    CPP file

    nTrakDB::nTrakDB()
    {
    db = QSqlDatabase::addDatabase("QSQLITE");
    QString dbName("E:/nTRak/mTrak.db");
    QFileInfo info(dbName);
    dbName = info.absoluteFilePath().replace("/", "\\");
    db.setDatabaseName(dbName);
    openDB();
    }

    QString nTrakDB::maxMsgId()
    {
    QString connection;
    QSqlQuery query(db);
    QString result;
    bool conStat = false;

    if(!db.isOpen())
    {
    conStat=openDB();
    QMessageBox qMsgBox;
    qMsgBox.setText("DB OPEN FAILED");
    qMsgBox.exec();
    }

    connection=db.connectionName();
    query.prepare("SELECT MAX(message_id) FROM gps_master" );

    if( !query.exec() ){
    QMessageBox qMsgBox;
    qMsgBox.setText("Quer EXEC FAILED");
    qMsgBox.exec();
    qFatal( "Failed to get MaximumID" );
    }

    }

    Few queries
    1. Where will the database file be created in symbian as i dont see getting created in Private folder. Instead it gets created in the folder where my sis is placed
    2. what does INSTALL option mean in pro file
    3. If DEPLOYMETN option is provided then, does the db file gets installed with the application to some location, to which location will the db file be created after isntallation

  10. #10
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Add existing Sqlite database to Qt project

    How is your database supposed to end up in "E:/nTRak/mTrak.db"?

  11. #11
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Add existing Sqlite database to Qt project

    Try this:

    database.sources = E:/nTRak/mTrak.db # (This assumes there's an "E" drive on your build system, and that's where the prototype DB is located)
    database.path = .
    DEPLOYMENT+= database


    QString dbName("mTrak.db");
    QFileInfo info(dbName);
    dbName = QDir.toNativeSeparators(info.absoluteFilePath());
    db.setDatabaseName(dbName);

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    11

    Re: Add existing Sqlite database to Qt project

    Dan ..

    Am still facing the problem after making the changes u mentioned....

    One more thing i noticed is,

    in pro file
    symbian {
    TARGET.UID3 = 0xeeb72120
    TARGET.CAPABILITY = Location\
    NetworkServices \
    ReadUserData \
    WriteUserData
    TARGET.EPOCSTACKSIZE = 0x14000
    TARGET.EPOCHEAPSIZE = 0x020000 0x800000
    }

    whereas in pkg file it is
    "E:/nTRak/nTRak.sis" - "c:\private\2002CCCE\import\nTRak.sis"
    @"E:/NokiaQtSDK/Symbian/SDK/smartinstaller.sis",(0x2002CCCD)


    UID seems to be different in 2 places... hope this matters, can you please point out ....

    where will the database file be installed ?

    Thanks ...

  13. #13
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Add existing Sqlite database to Qt project

    The DB file should be installed to c:\private\EEB72120\mTrak.db (assuming you choose to install on the "C" drive), and c:\private\EEB72120 is where the "current directory" should be set when your application is entered.

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    11

    Re: Add existing Sqlite database to Qt project

    Hi Dan

    The file is not installed in c:\private\EEB72120\mTrak.db and thats where even i was expecting. It didnt.
    Currently i am copying my sis application to C:\Ntrak. Is that correct ... IF i run it from Qt Creator, then where is it installed to ?

    I have set the current directory in the applicaiton constructor like this

    NtrakDB::NtrakDB()
    {
    // Moving the database open
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("mTrak.db");
    QDir dir(":/Private/EEB72120");
    QDir::setCurrent(dir.absolutePath);

    openDB();
    }
    Anything missing in my pro file .
    QT += core gui \
    sql
    CONFIG += mobility
    MOBILITY = location \
    messaging

    database.sources = "mTrak.db"
    database.path = .
    DEPLOYMENT += database

    symbian {
    TARGET.UID3 = 0xef8832d0
    TARGET.CAPABILITY += Location\
    NetworkServices \
    ReadUserData \
    WriteUserData

    TARGET.EPOCSTACKSIZE = 0x14000
    TARGET.EPOCHEAPSIZE = 0x020000 0x800000
    }
    Last edited by reachsakthi; 2011-05-08 at 01:48.

  15. #15
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Add existing Sqlite database to Qt project

    Quote Originally Posted by reachsakthi View Post
    Hi Dan

    The file is not installed in c:\private\EEB72120\mTrak.db and thats where even i was expecting. It didnt.
    Currently i am copying my sis application to C:\Ntrak. Is that correct ... IF i run it from Qt Creator, then where is it installed to ?
    I'm not understanding you. Where you copy your sis to shouldn't make any difference, so long as you can install it somehow.
    I have set the current directory in the applicaiton constructor like this

    NtrakDB::NtrakDB()
    {
    // Moving the database open
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("mTrak.db");
    QDir dir(":/Private/EEB72120");
    QDir::setCurrent(dir.absolutePath);

    openDB();
    }
    You shouldn't need to set the directory -- the default working directory, as I said, will be in your private directory. And you can always explicitly set it there with QDir::homePath(). And I believe using the ":" leading path character in Qt causes it to look in your resource files, not the file system -- it doesn't have the same meaning as in the package file.

    Do this: Add output statements of some sort (eg, write to a label) to print out what your current directory is, what path name is constructed for the DB file, QFileInfo::exists, etc.

Similar Threads

  1. Carbide 2.4 bug - does not add files to Qt project
    By tamhanna in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 8
    Last Post: 2010-02-27, 17:14
  2. Replies: 5
    Last Post: 2009-03-05, 10:17
  3. Add new build configurations to existing project for new SDKs in Carbide.c++. How?
    By dschwabe in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2006-11-09, 04:32
  4. How to add existing files(.cpp;.h) to a project in Carbide.c++?
    By zhentao_huang in forum Carbide.c++ IDE and plug-ins (Closed)
    Replies: 1
    Last Post: 2006-08-29, 06:55

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×