Results 1 to 4 of 4

Thread: steps to connect to SQLite from Q

  1. #1
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default steps to connect to SQLite from Q

    I m using Qt 4.4.3 on windows
    I want to use SQLite database for my application.
    I have installed SQLIte Maestro and created database.

    could anyone tell me steps to connect to SQLite from Qt.......

    do I need to configure any SQLite optins..??

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: steps to connect to SQLite from Q

    Take a look at Qt examples. There is a fiile called "examples/sql/connection.h".

    In your case you will have to specify a valid path to your db instead of ":memory:".
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Jun 2009
    Location
    Bato, Leyte, Philippines
    Posts
    10
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: steps to connect to SQLite from Q

    Try this.

    // Declares a database object
    QSqlDatabase db

    // opens a database connection for sqlite database
    db = QSqlDatabase::addDatabase("QSQLITE");

    // creates/opens a database genes.db in home directory of your application.
    db.setDatabaseName("genes.db");

  4. #4
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: steps to connect to SQLite from Q

    Part example:

    Qt Code:
    1. #define DBHOST "127.0.0.1" //for localhost loop
    2. #define DBDRIVER "QSQLITE" //if use other DB need the driver with dll files
    3. #define DBNAME "DB.db3" //name used for the database file
    4. //#define
    5.  
    6. //constructor
    7. Database::Database() {
    8. createConnectiontoSqlite();
    9. }
    10. ....
    11.  
    12. bool Database::createConnectiontoSqlite() {
    13. QSqlDatabase db = QSqlDatabase::addDatabase(DBDRIVER);// changed "QSQLITE" with define DBDRIVER
    14. db.setHostName(DBHOST);
    15. db.setDatabaseName(DBNAME); //if the dababase does not exist he will create it
    16. //the database name is also the name of the file in the working directory
    17. // db.setUserName("user");//username and password does not affect SQLITE db
    18. // db.setPassword("pass");
    19.  
    20. if (!db.open()) {
    21. QMessageBox::critical(0, QObject::tr("DB Error - CreateConnection()"),
    22. db.lastError().text());
    23. return false;
    24. }
    25. return true;
    26.  
    27. }
    28.  
    29. void Database::createTables() {
    30. QSqlQuery query;
    31. //main Tables
    32. // table
    33. query.exec(
    34. "create table table1(id int primary key, name varchar(20), variable int, company int)");
    35. query.exec("insert into table1 values(1, 'Default', 1, 1)");
    36. query.exec("insert into table1 values(2, 'blabla', 2, 1)");
    37. .....
    38.  
    39.  
    40. qDebug() << "dB created "; //need to set up debug in the .pro file
    To copy to clipboard, switch view to plain text mode 

    hope this help

Similar Threads

  1. SQLITE include error
    By Qt Coder in forum Qt Programming
    Replies: 1
    Last Post: 18th June 2009, 10:13
  2. Application only 'animates' once
    By arkain in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2009, 08:09
  3. qt how to connect to sqlite db file
    By TomASS in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2009, 09:47
  4. sqlite version in Qt
    By janus in forum Newbie
    Replies: 4
    Last Post: 5th February 2009, 14:18
  5. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 08:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.