Results 1 to 3 of 3

Thread: qt how to connect to sqlite db file

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default qt how to connect to sqlite db file

    Hello, I have a sqlite db file, it name is "tomek.sqlite". In this db I have a one table name: "am_tankowania"

    In code, I put:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2.  
    3. db.setDatabaseName("tomek.sqlite");
    4. if(db.open()) QMessageBox::information(0,"Ok","Connection ok");
    5. else QMessageBox::information(0,"Error","some error.");
    To copy to clipboard, switch view to plain text mode 

    The MessageBox ok - connection ok show. Now, I have a query:

    select * from am_tankowania:

    Qt Code:
    1. QSqlQuery query("SELECT * am_tankowania");
    2. if (query.lastError().isValid()) QMessageBox::critical(0,"Error",query.lastError().text() );
    To copy to clipboard, switch view to plain text mode 

    I see an error:
    no such table table: am_tankowania Unable to execute statement :/

    What i'm doing wrong?

    Thank you.

  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: qt how to connect to sqlite db file

    Well, this might be because db.setDatabaseName("tomek.sqlite"); points to a missing db file called tomek.sqlite. This should be a relative path. The reason is that probably your db file is not in the same directory as your executable.

    The MessageBox ok - connection ok show.
    It will always show ok. Because if there is no such file it will create it and open it (empty though).
    Last edited by lyuts; 22nd April 2009 at 09:51.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qt how to connect to sqlite db file

    this query looks wrong
    Qt Code:
    1. QSqlQuery query("SELECT * am_tankowania");
    To copy to clipboard, switch view to plain text mode 
    change it to
    Qt Code:
    1. QSqlQuery query("SELECT * FROM am_tankowania");
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  2. Can you specify a file engine?
    By skimber in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2008, 15:54
  3. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  4. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

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.