Results 1 to 15 of 15

Thread: QT - SQLite Connection

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2007
    Location
    Cluj Napoca, Romania
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hello, I'm sorry I can't help you with your problem, but I just wanted to ask you if you can provide a link to your source code because I am planning on using SQLite in one of my projects and it would be very useful to have some code as reference.
    Only if you are not against the idea.

    Thanks.
    May the source be with you!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Quote Originally Posted by balazsbela View Post
    I am planning on using SQLite in one of my projects and it would be very useful to have some code as reference.
    Take a look at examples in examples/sql/ directory. They all use SQLite.

  3. #3
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,
    This is the code for create a conection between QT and SQLite and read data from table.


    class Manager : public QMainWindow, private Ui::MainWindow
    {
    Q_OBJECT
    QSqlDatabase db;
    QSqlQuery query;

    public:
    Manager();
    ~Manager();
    bool createConnection();
    void LoadData();

    public slots:
    void play1();
    void stop1();
    void pause1();
    protected:
    };


    bool Manager::createConnection()
    {
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("ManagerDB.db");
    if (!db.open()) {
    QMessageBox::critical(0, qApp->tr("Cannot open database"),
    qApp->tr("Unable to establish a database connection.\n"
    "This program needs SQLite support. Please read "
    "the Qt SQL driver documentation for information how "
    "to build it.\n\n"
    "Click Cancel to exit."), QMessageBox::Cancel);
    return false;
    }
    return true;
    }


    void Manager::LoadData()
    {
    QSqlQuery query1 ("select * from TreeList" );
    while (query1.next()) {
    QString country = query1.value(2).toString();
    qDebug( query1.value(2).toString() );
    }
    }

  4. #4
    Join Date
    Nov 2006
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,

    where do you close your database connection? You have to use the QSqlDatabase close() function. In your case: db.close(). Put this in the destructor of your "Manager" class and the problem should be solved.

    regds
    big4mil

  5. #5
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,
    I give it on destructor. But the problem is that, the destructor is not working. How can i do that. I want to call the destructor when I close the Mainwindow. How can I do that?
    Please help me....

  6. #6
    Join Date
    Nov 2006
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    your destructor is called automatically when your mainwindow object is destroyed. How and where do you create your mainwindow object and how does your destructor looks like? Please paste your code, so we can help you.

  7. #7
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    Hi,
    Thankyou for your help.
    I solve the problum by setting this attribute on the constructor of main window.

    setAttribute(Qt::WA_DeleteOnClose);

    After setting this, the destructor is working when I close the main window.

  8. #8
    Join Date
    Nov 2006
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    and your problem is fixed?

  9. #9
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QT - SQLite Connection

    This is described in the documentation:
    http://doc.trolltech.com/4.3/qsqldat...removeDatabase

  10. #10
    Join Date
    Nov 2006
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT - SQLite Connection

    yeah, but that's not needed in that case. The call of the database close function will do it.

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. Replies: 3
    Last Post: 2nd August 2007, 21:28
  3. How do I keep the client connection open ?
    By probine in forum Newbie
    Replies: 2
    Last Post: 25th March 2006, 19:06
  4. Can I launch a dial-up connection in Windows?
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 9th February 2006, 12:32

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
  •  
Qt is a trademark of The Qt Company.