Results 1 to 12 of 12

Thread: Help with running a select statement from a button click

  1. #1
    Join Date
    Nov 2010
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Help with running a select statement from a button click

    Hi everyone

    This is my first post so wish me luck!

    I am new to C++ and QT, i have tried to write a small application that will search an sqlite database. But i cannot figure out how to run a select statement againts the database from a pushButton_clicked

    Any help would really get me started, thanks in advance.



    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. void on_pushButton_clicked();
    19.  
    20. private:
    21. Ui::MainWindow *ui;
    22.  
    23. private slots:
    24.  
    25. void on_pushButton_2_clicked();
    26. };
    27.  
    28. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 




    (mainwindow.cpp)


    Qt Code:
    1. void MainWindow:: on_pushButton_clicked()
    2. {
    3.  
    4.  
    5. bool DatabaseManager::getPerson(int ID, PersonData*& PERSON)
    6. {
    7. bool ret = false;
    8.  
    9. QSqlQuery query(QString("select * from PERSON where ID = %1").arg(ID));
    10. if (query.next())
    11. {
    12. PERSON->ID = query.value(0).toInt();
    13. PERSON->FIRSTNAME = query.value(1).toString();
    14. PERSON->LASTNAME = query.value(2).toString();
    15. PERSON->AGE = query.value(3).toInt();
    16. ret = true;
    17. }
    18.  
    19. return ret;
    20. }
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 





    dal.h

    Qt Code:
    1. #ifndef DAL_H
    2. #define DAL_H
    3.  
    4. #include <QObject>
    5. #include <QtSql/QSqlDatabase>
    6. #include <QtSql/QSqlError>
    7. #include <QtSql/QSqlQuery>
    8. #include <QString>
    9.  
    10. class PersonData
    11. {
    12. public:
    13. int ID;
    14. QString FIRSTNAME;
    15. QString LASTNAME;
    16. int AGE;
    17. };
    18.  
    19.  
    20. class DatabaseManager : public QObject
    21. {
    22. public:
    23. DatabaseManager(QObject *parent = 0);
    24. ~DatabaseManager();
    25.  
    26. public:
    27. bool openDB();
    28. bool getPerson(int ID, PersonData*& PERSON);
    29.  
    30. private:
    31. };
    32.  
    33. #endif // DAL_H
    To copy to clipboard, switch view to plain text mode 
    Reply With Quote




    dal.cpp

    Qt Code:
    1. #include "dal.h"
    2. #include <QtSql/QSqlDatabase>
    3. #include <QtSql/QSqlError>
    4. #include <QtSql/QSqlQuery>
    5. #include <QString>
    6. #include <mainwindow.h>
    7. #include <ui_mainwindow.h>
    8.  
    9.  
    10. MainWindow::MainWindow(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::MainWindow)
    13. {
    14. ui->setupUi(this);
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22.  
    23. bool DatabaseManager:penDB()
    24. {
    25. // Find QSLite driver
    26. db = QSqlDatabase::addDatabase("QSQLITE");
    27.  
    28. #ifdef Q_OS_LINUX
    29. // NOTE: We have to store database file into user home folder in Linux
    30. QString path(QDir::home().path());
    31. path.append(QDir::separator()).append("mydb.sqlite");
    32. path = QDir::toNativeSeparators(path);
    33. db.setDatabaseName(path);
    34. #else
    35. // NOTE: File exists in the application private folder, in Symbian Qt implementation
    36. db.setDatabaseName("mydb.sqlite");
    37. #endif
    38.  
    39. // Open databasee
    40. return db.open();
    41. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 15th November 2010 at 22:01.

  2. #2
    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: Help with running a select statement from a button click

    Hi,

    several notes:

    • What does not work? What does the error say. See QSqlQuery::lastError().
    • We have [CODE] tags here to make source code better to read.
    • Do not store a private member to a QSqlDatabase. Use the global (singleton) QSqlDatabase::database() in a local scope where you need it (so that is gets destroyed right afterwards).
    • Even if it isn't a problem here, have a look at QSqlQuery::prepare().
    • Don't use "*" and then address the fields on there index. Databases could (even if it is not likely) return the fields in a different order than you expect. So better use something like "SELECT id, name, foo, bar FROM ..." then you can be sure about the order.


    that's all for now

  3. #3
    Join Date
    Nov 2010
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Help with running a select statement from a button click

    Quote Originally Posted by Lykurg View Post
    Hi,

    several notes:

    • What does not work? What does the error say. See QSqlQuery::lastError().
    • We have [CODE] tags here to make source code better to read.
    • Do not store a private member to a QSqlDatabase. Use the global (singleton) QSqlDatabase::database() in a local scope where you need it (so that is gets destroyed right afterwards).
    • Even if it isn't a problem here, have a look at QSqlQuery::prepare().
    • Don't use "*" and then address the fields on there index. Databases could (even if it is not likely) return the fields in a different order than you expect. So better use something like "SELECT id, name, foo, bar FROM ..." then you can be sure about the order.


    that's all for now


    Hi Lykurg, thank you very much for your reply, i will try and reply best i can.

    * What does not work? What does the error say. See QSqlQuery::lastError().

    There are no errors at moment as the application just starts up and when i click on the pushButton it does nothing which is correct as i wanted the pushButton to connect to a sqlite database and run a sql statment and display the results in the listwidget

    As for your other recommendations i will try and slowly digest so i can try and implement into the code. If in the meantime if you could recommend you advise a way (Code examples) i could achieve my goal i would really appriciate it.

    The main issue i am having here is how to get text string from a lineedit widget and use as part of an sql statement to search a database.

    Thanks again for your help, much appriciated.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help with running a select statement from a button click

    It helps if you paste the actual code that is causing issues. What you posted won't compile (with the definition of getPerson inside the method on_pushButton_Clicked()). Assuming your code looks more like:
    Qt Code:
    1. void MainWindow:: on_pushButton_clicked()
    2. {
    3. // empty?
    4. }
    5.  
    6. bool DatabaseManager::getPerson(int ID, PersonData*& PERSON)
    7. {
    8. bool ret = false;
    9.  
    10. QSqlQuery query(QString("select * from PERSON where ID = %1").arg(ID));
    11. if (query.next())
    12. {
    13. PERSON->ID = query.value(0).toInt();
    14. PERSON->FIRSTNAME = query.value(1).toString();
    15. PERSON->LASTNAME = query.value(2).toString();
    16. PERSON->AGE = query.value(3).toInt();
    17. ret = true;
    18. }
    19. return ret;
    20. } // end of the DatabaseManager::getPerson() definition
    To copy to clipboard, switch view to plain text mode 
    Where and how do you call getPerson()?

  5. #5
    Join Date
    Nov 2010
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Help with running a select statement from a button click

    Quote Originally Posted by ChrisW67 View Post
    It helps if you paste the actual code that is causing issues. What you posted won't compile (with the definition of getPerson inside the method on_pushButton_Clicked()). Assuming your code looks more like:
    Qt Code:
    1. void MainWindow:: on_pushButton_clicked()
    2. {
    3. // empty?
    4. }
    5.  
    6. bool DatabaseManager::getPerson(int ID, PersonData*& PERSON)
    7. {
    8. bool ret = false;
    9.  
    10. QSqlQuery query(QString("select * from PERSON where ID = %1").arg(ID));
    11. if (query.next())
    12. {
    13. PERSON->ID = query.value(0).toInt();
    14. PERSON->FIRSTNAME = query.value(1).toString();
    15. PERSON->LASTNAME = query.value(2).toString();
    16. PERSON->AGE = query.value(3).toInt();
    17. ret = true;
    18. }
    19. return ret;
    20. } // end of the DatabaseManager::getPerson() definition
    To copy to clipboard, switch view to plain text mode 
    Where and how do you call getPerson()?


    Sorry if i am not explaining the issue very well. You are right the code would not compile with the getPerson inside the puchButton_clicked.

    Is there some way i can do this i.e. run that same code but from that click button?

    Thanks in advance

  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: Help with running a select statement from a button click

    Write the code into on_pushButton_clicked() or call the function from there, or make getPerson a slot and connect it with the buttons click signal.

  7. #7
    Join Date
    Nov 2010
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Help with running a select statement from a button click

    Hi Lykurg

    You have hit the nail on the head, could you maybe provide an example of how i could call that function from the pushButton, this would most definatley put me on the right track as this is what i am trying to do.

    Appriciate your patience with me, thanks again.

  8. #8
    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: Help with running a select statement from a button click

    Qt Code:
    1. void MainWindow:: on_pushButton_clicked()
    2. {
    3. YourDatabaseManager->getPerson(/*...*/); // like you would call any other function
    4. }
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Nov 2010
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Help with running a select statement from a button click

    [CODE]
    Quote Originally Posted by Lykurg View Post
    Qt Code:
    1. void MainWindow:: on_pushButton_clicked()
    2. {
    3. YourDatabaseManager->getPerson(/*...*/); // like you would call any other function
    4. }
    To copy to clipboard, switch view to plain text mode 



    Hi again

    I have tried adding DatabaseManager->getPerson(); to pushButton clicked and i get the below error

    unexpected unqualified '->' token


    I suspected that would cause a problem as the autocomplete code feature in Qt creator didnt auto fill whille i was typing it.

    Am i missing something obvious.

  10. #10
    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: Help with running a select statement from a button click

    I would really, really, reaaally, really suggest that you learn a tiny bit of C++ before you start developing a real program. Your "problem" has nothing to do with Qt, it's simply that you don't know how to use C++ and it's not something we can teach you here.
    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.


  11. #11
    Join Date
    Nov 2010
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Help with running a select statement from a button click

    Thanks for everyone you replied i have started reading alot more on C++.

    Could anyone please explain to me the below.

    Qt Code:
    1. bool getPerson(int ID, PersonData*& PERSON);
    To copy to clipboard, switch view to plain text mode 

    I know that * and & is used as pointers and are put after the text. But what is *&

    Thanks in advance.

  12. #12
    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: Help with running a select statement from a button click

    It's a reference to a pointer and I have never seen any practical use of it.
    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.


Similar Threads

  1. How can I know which button click.
    By electronicboy in forum Qt Programming
    Replies: 10
    Last Post: 4th October 2009, 14:27
  2. button click in webview
    By mind_freak in forum Qt Programming
    Replies: 1
    Last Post: 29th September 2009, 13:48
  3. QSqlQuery problem with SELECT statement
    By virtuosite in forum Newbie
    Replies: 2
    Last Post: 31st August 2009, 08:13
  4. SQL Select statement with binary comparison
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 21st June 2009, 02:33
  5. Replies: 6
    Last Post: 5th June 2009, 09:38

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.