Results 1 to 3 of 3

Thread: Best way to open database

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Best way to open database

    Good day, friends..

    I want to exchange some ideas. I have a class to connect to the database Firebird (below). All classes that need access to the database is inherited this class. What is the best way to make connection to the database? Am Ia the connection only at the beginning of the application and closing at the end?

    Qt Code:
    1. #include "Db.h"
    2.  
    3. const QString DB::UserNameDB = "SYSDBA";
    4. const QString DB::PasswordDB = "123456";
    5. QString DB::Servidor = "NULL";
    6. QString DB::Database = "NULL";
    7. bool DB::DBConfig = false;
    8.  
    9.  
    10. DB::DB(QString driver)
    11. :Driver(driver)
    12. {
    13. }
    14.  
    15. DB::~DB()
    16. {
    17. }
    18.  
    19. void DB::OpenDB()
    20. {
    21. ConnDB();
    22. }
    23.  
    24. void DB::CloseDB()
    25. {
    26. db.close();
    27. }
    28.  
    29. void DB::ConnDB()
    30. {
    31. if (!db.contains(Driver))
    32. {
    33. db = QSqlDatabase::addDatabase(Driver);
    34. db.setHostName(Servidor);
    35. db.setDatabaseName(Database);
    36. db.setUserName(UserNameDB);
    37. db.setPassword(PasswordDB);
    38. DBConfig = true;
    39. }
    40.  
    41. db.open();
    42. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,

    Marcelo E. Geyer
    Brazil/RS

  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: Best way to open database

    I don't know what the "best way" is, but the contents of your ConnDB() method looks fine. And yes, you only need to open the connection once before you start using the database and it will be automatically closed when you exit the application.

  3. The following user says thank you to wysota for this useful post:

    estanisgeyer (8th February 2008)

  4. #3
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Lightbulb Re: Best way to open database

    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4. #include <QMessageBox>
    5. #include <QSqlDatabase>
    6. #include <QSqlError>
    7.  
    8. static bool createConnection(QString driver= "QMYSQL", QString dbName= "VistaPenguin", QString user= "root", hostName="localhost")
    9. {
    10. QSqlDatabase db = QSqlDatabase::addDatabase(driver);
    11. db.setDatabaseName(dbName);
    12. db.setUserName( user );
    13. db.setHostName( hostName );
    14. if (!db.open()) {
    15. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    16. qApp->tr("Unable to establish a database connection.\n"
    17. "Click Cancel to exit."), QMessageBox::Cancel);
    18. return false;
    19. }
    20.  
    21. return true;
    22. }
    23.  
    24. #endif
    To copy to clipboard, switch view to plain text mode 

    I am using above and call only once in main.cpp.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  5. The following user says thank you to ashukla for this useful post:

    estanisgeyer (8th February 2008)

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. Replies: 4
    Last Post: 23rd November 2010, 17:15
  3. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 14:31
  4. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 17:50
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

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.