Results 1 to 9 of 9

Thread: Basic Qt Sql Question

  1. #1
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Basic Qt Sql Question

    Hello,

    I was trying to build a simple connection to my database that i created recently. But it seems my code doesn't work or sth else wrong. Actually I want to show the values from database file.

    Here's the code :


    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QtSql>
    3. #include <QtDebug>
    4. #include <iostream>
    5. using namespace std;
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QCoreApplication a(argc, argv);
    10.  
    11.  
    12.  
    13. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    14.  
    15. db.setConnectOptions();
    16.  
    17. db.setDatabaseName("deneme");
    18. db.setHostName("localhost");
    19. db.setUserName("root");
    20. db.setPassword("2245009");
    21.  
    22.  
    23. if(db.open())
    24. {
    25. cout<<"opened"<<endl;
    26.  
    27. QSqlQuery qry;
    28. if(qry.exec("SELECT *FROM `tablo` 1"))
    29.  
    30. {
    31. while(qry.next())
    32. {
    33. //qDebug() << qry.value(1).toString();
    34. qDebug() << qry.value(1);
    35. }
    36. }
    37. else
    38. {
    39. qDebug() << "error = " << db.lastError().text();
    40. }
    41. cout<<"closing"<<endl;
    42. db.close();
    43.  
    44. }
    45. else
    46. {
    47. qDebug() << "error = " << db.lastError();
    48. }
    49.  
    50. return a.exec();
    51. }
    To copy to clipboard, switch view to plain text mode 



    The output is like that :

    Starting /home/onur/dbapp-build-desktop/dbapp...
    opened
    closing
    error = " "
    It shows an empty error. I didn't understand anything because of being a newbie. Any advices?

    Thanks in Advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Basic Qt Sql Question

    Do you have a SQLite database file called "deneme" in your current working directory?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Basic Qt Sql Question

    thank you for your reply. i didn't have. now changed, but still the same problem.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Basic Qt Sql Question

    Try "qry.lastError().text()" @ line 39 in lieu of "db.lastError().text()"

  5. #5
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Basic Qt Sql Question

    now the error changes like:

    error = "near "1": syntax error Unable to execute statement"

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Basic Qt Sql Question

    What wysota was trying to say to you is: You are using the wrong database driver for MySql. This is most probably also the reason why the query throws an error. Not all database systems understand the same SQL commands.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Basic Qt Sql Question

    Quote Originally Posted by sidenelen View Post
    now the error changes like:
    And if you correct the syntax? Does it work then?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic Qt Sql Question

    Yours SQL query is bad
    Qt Code:
    1. SELECT * FROM `tablo` 1
    To copy to clipboard, switch view to plain text mode 
    It should be
    Qt Code:
    1. SELECT * FROM `tablo`
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. SELECT * FROM `tablo` WHERE 1
    To copy to clipboard, switch view to plain text mode 
    Of course, the latter figure does not make sense.

  9. #9
    Join Date
    Mar 2012
    Posts
    10
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Basic Qt Sql Question

    @Lesiok
    Yeah, I corrected the code as you said and it works! Thank you all guys

Similar Threads

  1. basic C++ question
    By Maluko_Da_Tola in forum Newbie
    Replies: 3
    Last Post: 24th August 2010, 09:29
  2. very basic Qt/c++ question
    By Maluko_Da_Tola in forum Newbie
    Replies: 2
    Last Post: 25th July 2010, 15:02
  3. A few basic question
    By salmanmanekia in forum Newbie
    Replies: 12
    Last Post: 17th June 2010, 08:46
  4. Basic question
    By giacomelli.fabio in forum Newbie
    Replies: 4
    Last Post: 18th December 2009, 01:12
  5. Using QSA: A very basic question
    By yogeshm02 in forum Newbie
    Replies: 3
    Last Post: 26th January 2006, 08:34

Tags for this Thread

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.