Results 1 to 12 of 12

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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