Results 1 to 10 of 10

Thread: Something good to read about QSqlDatabase

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Something good to read about QSqlDatabase

    Is there any good tutorial on QSqlDatabase?
    I find it a little tricky to understand how to use it.

  2. #2
    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: Something good to read about QSqlDatabase

    what are you confused?

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Something good to read about QSqlDatabase

    Well it started with
    db->isValid() // this works
    db->isOpen() // this fails with segfault

    so how do I check if I have a connection to the database?

    Also, is it possible to create more persistant connections? I use PostgreSQL.

  4. #4
    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: Something good to read about QSqlDatabase

    Quote Originally Posted by Morea View Post
    Well it started with
    db->isValid() // this works
    db->isOpen() // this fails with segfault

    so how do I check if I have a connection to the database?
    can you show compilable code with this seg-fault?

    Quote Originally Posted by Morea View Post
    Also, is it possible to create more persistant connections? I use PostgreSQL.
    yes, it is. you can create several database connection using,
    Qt Code:
    1. QSqlDatabase QSqlDatabase::addDatabase ( const QString & type, const QString & connectionName = QLatin1String( defaultConnection ) )
    To copy to clipboard, switch view to plain text mode 
    but you must to set unique connectionName for needed connection

  5. #5
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Something good to read about QSqlDatabase

    The code would be this (and the apply code is called when clicking on the button
    Qt Code:
    1. #ifndef CONFIG_H
    2. #define CONFIG_H
    3.  
    4. #include <QtSql>
    5. #include <QtCore>
    6. #include "ui_config.h"
    7.  
    8. class Config : public QDialog
    9. {
    10. Q_OBJECT
    11. public:
    12. Config(QWidget * parent = 0);
    13. private:
    14. Ui::Config ui;
    15. QSettings* settings;
    16. bool createDatabaseTable();
    17. private slots:
    18. void apply();
    19. void noapply();
    20. public slots:
    21. };
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "config.h"
    2. #include <QtGui>
    3. #include <QtCore>
    4. #include "global.h"
    5.  
    6. Config::Config(QWidget *parent)
    7. : QDialog(parent, 0)
    8. {
    9. setModal(true);
    10. ui.setupUi(this);
    11. connect(ui.apply,SIGNAL(clicked()),this,SLOT(apply()));
    12. connect(ui.noapply,SIGNAL(clicked()),this,SLOT(noapply()));
    13.  
    14. settings = new QSettings(ORGANISATIONNAME,APPLICATIONNAME,this);
    15. // Read configured values, if they exist
    16. ui.hostname->setText(settings->value("hostname","").toString());
    17. ui.port->setText(settings->value("port","").toString());
    18. ui.database->setText(settings->value("database","").toString());
    19. ui.username->setText(settings->value("username","").toString());
    20. ui.password->setText(settings->value("password","").toString());
    21.  
    22. db->QSqlDatabase::addDatabase("QPSQL");
    23. qDebug()<<db;
    24. qDebug()<<db->isValid();
    25. qDebug()<<"constructor done";
    26. }
    27.  
    28. bool Config::createDatabaseTable()
    29. {
    30. bool ok=false;
    31. if (! db->isOpen())
    32. {
    33. db->setHostName(settings->value("hostname","").toString());
    34. QString tmp = settings->value("port").toString();
    35. if (tmp != "");
    36. db->setPort(tmp.toInt());
    37. db->setDatabaseName(settings->value("database","").toString());
    38. db->setUserName(settings->value("username","").toString());
    39. db->setPassword(settings->value("password","").toString());
    40. ok = db->open();
    41. }
    42. else
    43. {
    44. ok=true;
    45. }
    46. if (ok)
    47. {
    48. QSqlQuery result = db->exec("SELECT * FROM DomainTable");
    49. if (result.isValid())
    50. {
    51. // table already exist.
    52. return true;
    53. }
    54. else
    55. {
    56. // goahead and try to create the database table
    57. return false;
    58. }
    59. }
    60. else
    61. {
    62. QMessageBox::critical( this, "Failed to connect to database", "Edit connection settings again and apply");
    63. return false;
    64. }
    65. }
    66.  
    67. void Config::noapply()
    68. {
    69. this->done(0);
    70. }
    71.  
    72. void Config::apply()
    73. {
    74. settings->setValue("hostname",ui.hostname->text());
    75. settings->setValue("database",ui.database->text());
    76. settings->setValue("port",ui.port->text());
    77. settings->setValue("username",ui.username->text());
    78. settings->setValue("password",ui.password->text());
    79. settings->sync();
    80. qDebug()<<"Appply";
    81. qDebug()<<"valid "<<db->isValid();
    82. //qDebug()<<"open "<<db->isOpen();
    83. qDebug()<<"continue";
    84. bool ok=false;
    85. if (! db->isOpen())
    86. {
    87. db->setHostName(settings->value("hostname","").toString());
    88. QString tmp = settings->value("port").toString();
    89. if (tmp != "");
    90. db->setPort(tmp.toInt());
    91. db->setDatabaseName(settings->value("database","").toString());
    92. db->setUserName(settings->value("username","").toString());
    93. db->setPassword(settings->value("password","").toString());
    94. ok = db->open();
    95. }
    96. else
    97. ok=true;
    98. if (ok)
    99. {
    100. ok = createDatabaseTable();
    101. if (ok)
    102. this->done(1);
    103. else
    104. QMessageBox::critical( this, "Failed to create the database table", "Check database log");
    105. }
    106. else
    107. {
    108. QMessageBox::critical( this, "Failed to connect to database", "Edit connection settings");
    109. }
    110. // else continue to work...
    111. }
    To copy to clipboard, switch view to plain text mode 






    Qt Code:
    1. <ui version="4.0" >
    2. <class>Config</class>
    3. <widget class="QDialog" name="Config" >
    4. <property name="geometry" >
    5. <rect>
    6. <x>0</x>
    7. <y>0</y>
    8. <width>561</width>
    9. <height>306</height>
    10. </rect>
    11. </property>
    12. <property name="sizePolicy" >
    13. <sizepolicy vsizetype="Fixed" hsizetype="Fixed" >
    14. <horstretch>0</horstretch>
    15. <verstretch>0</verstretch>
    16. </sizepolicy>
    17. </property>
    18. <property name="windowTitle" >
    19. <string>Database settings</string>
    20. </property>
    21. <widget class="QPushButton" name="apply" >
    22. <property name="geometry" >
    23. <rect>
    24. <x>70</x>
    25. <y>230</y>
    26. <width>81</width>
    27. <height>28</height>
    28. </rect>
    29. </property>
    30. <property name="text" >
    31. <string>Apply</string>
    32. </property>
    33. </widget>
    34. <widget class="QPushButton" name="noapply" >
    35. <property name="geometry" >
    36. <rect>
    37. <x>250</x>
    38. <y>230</y>
    39. <width>188</width>
    40. <height>28</height>
    41. </rect>
    42. </property>
    43. <property name="text" >
    44. <string>Do NOT apply values above</string>
    45. </property>
    46. </widget>
    47. <widget class="QLineEdit" name="username" >
    48. <property name="geometry" >
    49. <rect>
    50. <x>100</x>
    51. <y>130</y>
    52. <width>451</width>
    53. <height>24</height>
    54. </rect>
    55. </property>
    56. </widget>
    57. <widget class="QLabel" name="label" >
    58. <property name="geometry" >
    59. <rect>
    60. <x>20</x>
    61. <y>40</y>
    62. <width>81</width>
    63. <height>18</height>
    64. </rect>
    65. </property>
    66. <property name="text" >
    67. <string>Hostname</string>
    68. </property>
    69. </widget>
    70. <widget class="QLabel" name="label_2" >
    71. <property name="geometry" >
    72. <rect>
    73. <x>20</x>
    74. <y>70</y>
    75. <width>62</width>
    76. <height>18</height>
    77. </rect>
    78. </property>
    79. <property name="text" >
    80. <string>Port</string>
    81. </property>
    82. </widget>
    83. <widget class="QLabel" name="label_3" >
    84. <property name="geometry" >
    85. <rect>
    86. <x>20</x>
    87. <y>100</y>
    88. <width>62</width>
    89. <height>18</height>
    90. </rect>
    91. </property>
    92. <property name="text" >
    93. <string>Database</string>
    94. </property>
    95. </widget>
    96. <widget class="QLabel" name="label_4" >
    97. <property name="geometry" >
    98. <rect>
    99. <x>20</x>
    100. <y>130</y>
    101. <width>81</width>
    102. <height>18</height>
    103. </rect>
    104. </property>
    105. <property name="text" >
    106. <string>Username</string>
    107. </property>
    108. </widget>
    109. <widget class="QLineEdit" name="hostname" >
    110. <property name="geometry" >
    111. <rect>
    112. <x>100</x>
    113. <y>40</y>
    114. <width>451</width>
    115. <height>24</height>
    116. </rect>
    117. </property>
    118. </widget>
    119. <widget class="QLineEdit" name="port" >
    120. <property name="geometry" >
    121. <rect>
    122. <x>100</x>
    123. <y>70</y>
    124. <width>113</width>
    125. <height>24</height>
    126. </rect>
    127. </property>
    128. </widget>
    129. <widget class="QLineEdit" name="database" >
    130. <property name="geometry" >
    131. <rect>
    132. <x>100</x>
    133. <y>100</y>
    134. <width>451</width>
    135. <height>24</height>
    136. </rect>
    137. </property>
    138. </widget>
    139. <widget class="QLineEdit" name="password" >
    140. <property name="geometry" >
    141. <rect>
    142. <x>100</x>
    143. <y>160</y>
    144. <width>451</width>
    145. <height>24</height>
    146. </rect>
    147. </property>
    148. </widget>
    149. <widget class="QLabel" name="label_5" >
    150. <property name="geometry" >
    151. <rect>
    152. <x>20</x>
    153. <y>160</y>
    154. <width>62</width>
    155. <height>18</height>
    156. </rect>
    157. </property>
    158. <property name="text" >
    159. <string>Password</string>
    160. </property>
    161. </widget>
    162. <widget class="QLabel" name="label_6" >
    163. <property name="geometry" >
    164. <rect>
    165. <x>40</x>
    166. <y>200</y>
    167. <width>501</width>
    168. <height>18</height>
    169. </rect>
    170. </property>
    171. <property name="text" >
    172. <string>Notice that hitting apply will access the database and create database tables!</string>
    173. </property>
    174. </widget>
    175. </widget>
    176. <tabstops>
    177. <tabstop>hostname</tabstop>
    178. <tabstop>port</tabstop>
    179. <tabstop>database</tabstop>
    180. <tabstop>username</tabstop>
    181. <tabstop>password</tabstop>
    182. <tabstop>apply</tabstop>
    183. <tabstop>noapply</tabstop>
    184. </tabstops>
    185. <resources/>
    186. <connections/>
    187. </ui>
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: Something good to read about QSqlDatabase

    the first:
    what is this?
    Qt Code:
    1. ...
    2. db->QSqlDatabase::addDatabase("QPSQL");
    3. ...
    To copy to clipboard, switch view to plain text mode 
    I didn't see where you create object using operator new.

    the second:
    you shouldn't use pointer to database, because QSqlDatabase class is shared.
    so, I suggest you to revise you code: refuse using pointer to database (but if you prefer
    to use pointer then you must use new operator)

    you can also not to keep variable for database like a class member, you can always get a needed database object using
    Qt Code:
    1. QSqlDatabase QSqlDatabase::database ( const QString & connectionName = QLatin1String( defaultConnection ), bool open = true )
    To copy to clipboard, switch view to plain text mode 

    conclusion:
    you can use the following code
    Qt Code:
    1. void someObject::initDatabse
    2. {
    3. QSqlDatabese db = QSqlDatabse::addDatabase("QPSQL", "myConnection");
    4. .....//do connection stuff
    5. }
    6.  
    7. void someObject::execQuery(const QString &queryText)
    8. {
    9. QSqlDatabse db = QSqlDatabase::database("myConnection");
    10. QSqlQuery query(db);
    11. query.prepare(queryText);
    12. ....
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Something good to read about QSqlDatabase

    • in Qt4 QSqlDatabase is a value type: no need to handle with pointers like QSqlDatabase*
    • you did not initialize your db pointer (well, you should replace it by a QSqlDatabase anyway)


    so replace QSqlDatabase *db; by QSqlDatabase db;
    and db->QSqlDatabase::addDatabase("QPSQL"); by db = QSqlDatabase::addDatabase("QPSQL");

    HTH

  8. #8
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Something good to read about QSqlDatabase

    I'm currently editing

  9. #9
    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: Something good to read about QSqlDatabase

    probaly you have set incorrect parameters for a database.
    Qt Code:
    1. #include <QApplication>
    2. #include <QtSql>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QSqlDatabase db2 = QSqlDatabase::addDatabase("QPSQL", "MyConnection");
    9. db2.setHostName("localhost");
    10. db2.setDatabaseName("curriculum");
    11. db2.setUserName("postgres");
    12. db2.setPassword("sysdba");
    13.  
    14. qDebug() << db2;//res == QSqlDatabase(driver=""QPSQL"", database=""curriculum"", host=""localhost"", port=-1, user=""postgres"", open=false)
    15. qDebug() << QSqlDatabase::connectionNames();//res == MyConnection
    16. QSqlDatabase db = QSqlDatabase::database("MyConnection");
    17. qDebug() << db;//res == QSqlDatabase(driver=""QPSQL"", database=""curriculum"", host=""localhost"", port=-1, user=""postgres"", open=true)
    18.  
    19.  
    20. return 0;
    21. }
    To copy to clipboard, switch view to plain text mode 
    this code works fine.

  10. #10
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Something good to read about QSqlDatabase

    with that code and setting port to -1 helped alot!

Similar Threads

  1. External Lib read from QBuffer on Calback problem
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2008, 19:43
  2. How to read CD with read?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 29th June 2007, 08:20
  3. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29

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.