Results 1 to 7 of 7

Thread: QSql Database

  1. #1
    Join Date
    May 2009
    Posts
    129

    Default QSql Database

    Hi All

    I want to use QSql in my application to call History..

    I have gone through the qt examples and documents and i tried small examples based the examples given with Sdk.

    I want to store call history..

    In examples i did like
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(":memory:");
    3. if (!db.open()) {
    4. return;
    5. }
    6. QSqlQuery query;
    7. //query.exec("create table yuvaraj ( id int , name varchar(20000))");
    8. query.exec("insert into yuvaraj values(1000,'"+QString(str)+"')");
    9. model = new QSqlRelationalTableModel(this);
    10. model->setTable("yuvaraj");
    11. model->select();
    12. QMessageBox::information(0,"1",QString("yuvarj"));
    13. mapper = new QDataWidgetMapper(this);
    14. mapper->setModel(model);
    15. mapper->addMapping(ui->textBrowser, model->fieldIndex("name"));
    16. QMessageBox::information(0,"2",QString("yuvarj"));
    17. mapper->toFirst();
    To copy to clipboard, switch view to plain text mode 

    there isan another way to create own database


    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    2. db.setHostName("customer");
    3. db.setDatabaseName("customdb");
    4. db.setUserName("yuvaraj");
    5. db.setPassword("yuvaraj");
    6. bool ok = db.open();
    To copy to clipboard, switch view to plain text mode 

    i have one doubt .which method i have to use.if declare this code at constructor every time database and table is creating..

    Where can i find good example based one QSql apart from examples.

    Can you please suggest me

    Thanks

    Yuvaraj R

  2. #2
    Join Date
    Apr 2009
    Posts
    21
    Thanked 3 Times in 2 Posts

    Default Re: QSql Database

    Here is my example:
    I create one thread for every database connection. That works nice! And the UI will not get blocked when there is a heavy database operation.

    the header:
    Qt Code:
    1. #ifndef DBTHREAD_H
    2. #define DBTHREAD_H
    3.  
    4. #include "WorkerThread.h"
    5. #include <QSqlDatabase>
    6.  
    7. class DbThread : public WorkerThread
    8. {
    9. public:
    10. DbThread(QObject *parent = 0,
    11. QThread::Priority priority = QThread::NormalPriority);
    12. virtual ~DbThread();
    13.  
    14. protected:
    15. void setDbServer
    16. (const char* dbtype, const char* connection,
    17. const char* hostname, const char* username,
    18. const char* password, int port);
    19. bool openDb(const char* dbname);
    20. void closeDb();
    21. QSqlDatabase& getDb(const char* connection);
    22.  
    23. private:
    24. };
    25.  
    26. #endif // DBTHREAD_H
    To copy to clipboard, switch view to plain text mode 

    the realization:
    Qt Code:
    1. #include "DbThread.h"
    2.  
    3. DbThread::DbThread(QObject *parent, QThread::Priority priority)
    4. : WorkerThread(parent, priority)
    5. {
    6.  
    7. }
    8.  
    9. DbThread::~DbThread()
    10. {
    11. }
    12.  
    13. void DbThread::setDbServer(const char* dbtype, const char* connection,
    14. const char* hostname, const char* username, const char* password, int port)
    15. {
    16. db = QSqlDatabase::addDatabase(dbtype, connection);
    17.  
    18. db.setHostName(hostname);
    19. db.setUserName(username);
    20. db.setPassword(password);
    21. db.setPort(port);
    22.  
    23. db = QSqlDatabase::database(connection, false);
    24.  
    25. DBHEX("add connection complete", connection);
    26. }
    27.  
    28. bool DbThread::openDb(const char* dbname)
    29. {
    30. db.setDatabaseName(dbname);
    31. return db.open();
    32. }
    33.  
    34. void DbThread::closeDb()
    35. {
    36. db.close();
    37. }
    38.  
    39. QSqlDatabase& DbThread::getDb(const char* connection)
    40. {
    41. return db;
    42. }
    To copy to clipboard, switch view to plain text mode 

    Hope it be useful for you
    Welcome to My Chinese Qt Blog (Google Translate Integrated) - http://www.xlrw.co.cc
    Dedicated to make Qt popular in Chinese-speaking society.

  3. #3
    Join Date
    Mar 2009
    Posts
    98
    Thanks
    3
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSql Database

    Can you also post your WorkerThread class?

  4. #4
    Join Date
    Apr 2009
    Posts
    21
    Thanked 3 Times in 2 Posts

    Default Re: QSql Database

    WorkerThread is my own Qt thread wrapper for my project. Every type of thread in my project will inherit it. The DbThread is one of the.

    Here is the code of WorkerThread:

    Qt Code:
    1. #ifndef WORKERTHREAD_H
    2. #define WORKERTHREAD_H
    3.  
    4. #include "common.h"
    5. #include <QThread>
    6. #include <QMutex>
    7. #include <QWaitCondition>
    8. #include <list>
    9. using namespace std;
    10. class Message;
    11. //class QEvent;
    12.  
    13. class WorkerThread : public QThread
    14. {
    15. public:
    16. WorkerThread(QObject *parent = 0,
    17. QThread::Priority priority = QThread::NormalPriority);
    18. ~WorkerThread();
    19. void QueueAction(Message& action);
    20.  
    21. protected:
    22. void run();
    23. QMutex mutex;
    24. QWaitCondition bufferEmpty;
    25. list<Message> m_listActionBuffer;
    26. Message* m_tempMsg;
    27. virtual void WorkerThreadMain(Message& action) = 0;
    28. void postEvent(Message* ev, uint32 type);
    29.  
    30. private:
    31. QThread::Priority m_priority;
    32. };
    33.  
    34. #endif // WORKERTHREAD_H
    35.  
    36. #include "WorkerThread.h"
    37. #include <QCoreApplication>
    38. #include <QEvent>
    39. #include "TEvent.h"
    40. #include "Message.h"
    41.  
    42. WorkerThread::WorkerThread(QObject *parent, QThread::Priority priority)
    43. :QThread(parent),m_priority(priority),m_tempMsg(NULL)
    44. {
    45.  
    46. }
    47.  
    48. WorkerThread::~WorkerThread()
    49. {
    50. if(NULL != m_tempMsg)
    51. delete m_tempMsg;
    52. }
    53.  
    54. void WorkerThread::QueueAction(Message& Action)
    55. {
    56. if(!isRunning()) {
    57. start(m_priority);
    58. }
    59.  
    60. mutex.lock();
    61. m_listActionBuffer.push_back(Action);
    62. mutex.unlock();
    63.  
    64.  
    65. mutex.lock();
    66. if(!m_listActionBuffer.empty())
    67. bufferEmpty.wakeOne();
    68. mutex.unlock();
    69. }
    70.  
    71. void WorkerThread::run() {
    72. DBHEX("worker thread start@", (int)this);
    73. for(;;) {
    74. mutex.lock();
    75. if(m_listActionBuffer.empty())
    76. bufferEmpty.wait(&mutex);
    77. mutex.unlock();
    78.  
    79. mutex.lock();
    80. Message Action = m_listActionBuffer.front();
    81. m_listActionBuffer.pop_front();
    82. mutex.unlock();
    83.  
    84. WorkerThreadMain(Action);
    85. }
    86. }
    87.  
    88. void WorkerThread::postEvent(Message* ev, uint32 type)
    89. {
    90. QEvent* qev= new TEvent((QEvent::Type)type, ev);
    91. QCoreApplication::postEvent(parent(), qev,Qt::HighEventPriority);
    92. }
    To copy to clipboard, switch view to plain text mode 

    And an example of one database connection thread:

    Qt Code:
    1. ///////////////////////////////////////////////////////////
    2. // BmDbThread.h
    3. // Implementation of the Class BmDbThread
    4. // Created on: 05-五月-2009 11:40:31
    5. // Original author: Tim Kuo
    6. ///////////////////////////////////////////////////////////
    7.  
    8. #if !defined(EA_6594F28D_DB37_4636_8095_CAF8A14A53D9__INCLUDED_)
    9. #define EA_6594F28D_DB37_4636_8095_CAF8A14A53D9__INCLUDED_
    10.  
    11. #include "DbThread.h"
    12. class Business;
    13. class BusinessType;
    14. class QByteArray;
    15. class Message;
    16. #include <list>
    17. using namespace std;
    18.  
    19. class BmDbThread : public DbThread
    20. {
    21. public:
    22. BmDbThread(QObject *parent = 0,
    23. QThread::Priority priority = QThread::NormalPriority);
    24. virtual ~BmDbThread();
    25.  
    26. protected:
    27. virtual void WorkerThreadMain(Message& action);
    28.  
    29. private:
    30. bool addBusiness(Business* data);
    31. bool addBusinessType(BusinessType* data);
    32. bool addImage(uint32 id, QByteArray& data);
    33. Business* getBusiness(uint32 id);
    34. list<BusinessType>* getBusinessTypes();
    35. QByteArray* getImage(uint32 id);
    36. bool modifyBusiness(Business* data);
    37. bool modifyBusinessType(BusinessType* data);
    38. bool removeBusiness(uint32 id);
    39. bool removeBusinessType(uint32 id);
    40. bool removeImage(uint32 id);
    41. list<Business>* getAllBusiness();
    42.  
    43. };
    44. #endif // !defined(EA_6594F28D_DB37_4636_8095_CAF8A14A53D9__INCLUDED_)
    45. ///////////////////////////////////////////////////////////
    46. // BmDbThread.cpp
    47. // Implementation of the Class BmDbThread
    48. // Created on: 05-五月-2009 11:40:31
    49. // Original author: Tim Kuo
    50. ///////////////////////////////////////////////////////////
    51.  
    52. #include "BmDbThread.h"
    53. #include "Business.h"
    54. #include "BusinessType.h"
    55. #include "Message.h"
    56. #include "messagedef.h"
    57. #include <QByteArray>
    58. #include <QSqlQuery>
    59. #include <QVariant>
    60. #include "dbquery.h"
    61.  
    62. BmDbThread::BmDbThread(QObject *parent, QThread::Priority priority)
    63. :DbThread(parent, priority)
    64. {
    65. setDbServer("QSQLITE", DBCONNECTION_BM, "", "", "", 0);
    66. }
    67.  
    68.  
    69. BmDbThread::~BmDbThread(){
    70.  
    71. }
    72.  
    73. void BmDbThread::WorkerThreadMain(Message& action)
    74. {
    75. DBHEX("bm db thread processing action: ", action.type());
    76. switch(action.type()) {
    77. case ACTION_GETBUSINESSTYPE:
    78. {
    79. m_tempMsg = new Message(EVENT_BUSINESSTYPE, getBusinessTypes());
    80. break;
    81. }
    82. case ACTION_GETALLBUSINESS:
    83. {
    84. m_tempMsg = new Message(EVENT_ALLBUSINESS, getAllBusiness());
    85. break;
    86. }
    87. case ACTION_GETBUSINESS:
    88. {
    89. uint32* id = static_cast<uint32*>(action.data());
    90. m_tempMsg = new Message(EVENT_BUSINESS, getBusiness(*id));
    91. delete id;
    92. break;
    93. }
    94. case ACTION_GETBUSINESSPIC:
    95. {
    96. uint32* id = static_cast<uint32*>(action.data());
    97. m_tempMsg = new Message(EVENT_BUSINESSPIC, getImage(*id));
    98. delete id;
    99. break;
    100. }
    101. case ACTION_ADDBUSINESS:
    102. {
    103. Business* business = static_cast<Business*>(action.data());
    104. QByteArray* image = static_cast<QByteArray*>(action.data2());
    105. Business* addedBusiness = NULL;
    106. m_tempMsg = new Message(EVENT_ADDBUSINESS);
    107. if(addBusiness(business)) {
    108. addedBusiness = getBusiness(business->id());
    109. m_tempMsg->setData(addedBusiness);
    110. if(NULL != addedBusiness && NULL != image && !image->isEmpty()) {
    111. if(addImage(addedBusiness->id(), *image))
    112. m_tempMsg->setData2(getImage(addedBusiness->id()));
    113. image->clear();
    114. }
    115. }
    116. delete business;
    117. delete image;
    118. break;
    119. }
    120. case ACTION_REMOVEBUSINESS:
    121. {
    122. uint32* id = static_cast<uint32*>(action.data());
    123. uint32* result = new uint32(*id);
    124. if(!removeBusiness(*id))
    125. *result = 0;
    126. m_tempMsg = new Message(EVENT_REMOVEBUSINESS, result);
    127. delete id;
    128. break;
    129. }
    130. case ACTOIN_MODIFYBUSINESS:
    131. {
    132. Business* toModify = static_cast<Business*>(action.data());
    133. QByteArray* image = static_cast<QByteArray*>(action.data2());
    134. Business* modified = NULL;
    135. m_tempMsg = new Message(EVENT_MODIFYBUSINESS);
    136. if(modifyBusiness(toModify)) {
    137. modified = getBusiness(toModify->id());
    138. m_tempMsg->setData(modified);
    139. if(NULL != image && !image->isEmpty() && toModify->id() != 0) {
    140. if(addImage(modified->id(), *image))
    141. m_tempMsg->setData2(getImage(modified->id()));
    142. }
    143. }
    144. delete toModify;
    145. break;
    146. }
    147. case ACTION_VIEWBUSINESSRECORD:
    148. {
    149. break;
    150. }
    151. case ACTION_SETBUSINESSTYPE:
    152. {
    153. list<BusinessType>* toSet =
    154. static_cast<list<BusinessType>*>(action.data());
    155. list<BusinessType>* result = new list<BusinessType>;
    156. list<BusinessType>::iterator it = toSet->begin();
    157. while(toSet->end() != it) {
    158. if(!addBusinessType(&(*it)))
    159. result->push_back(*it);
    160. it++;
    161. }
    162. delete toSet;
    163. m_tempMsg = new Message(EVENT_SETBUSINESSTYPE, result);
    164. break;
    165. }
    166. case ACTION_REMOVEBUSINESSTYPE:
    167. {
    168. list<BusinessType>* toRemove =
    169. static_cast<list<BusinessType>*>(action.data());
    170. list<BusinessType>* result = new list<BusinessType>;
    171. list<BusinessType>::iterator it = toRemove->begin();
    172. while(toRemove->end() != it) {
    173. if(!removeBusinessType(it->getId()))
    174. result->push_back(*it);
    175. it++;
    176. }
    177. delete toRemove;
    178. m_tempMsg = new Message(EVENT_REMOVEBUSINESSTYPE, result);
    179. break;
    180.  
    181. }
    182. default: break;
    183. }
    184. if(NULL != m_tempMsg) {
    185. postEvent(m_tempMsg, EventDb);
    186. m_tempMsg = NULL;
    187. }
    188. }
    To copy to clipboard, switch view to plain text mode 

    I omitted other function in BmDbThread
    A concrete database thread class realize the virtual void WorkerThreadMain(Message& action),and code to do what it wants when the actions come into QueueAction(Message& action)
    Last edited by freezeblue; 18th July 2009 at 16:02.
    Welcome to My Chinese Qt Blog (Google Translate Integrated) - http://www.xlrw.co.cc
    Dedicated to make Qt popular in Chinese-speaking society.

  5. #5
    Join Date
    May 2009
    Posts
    129

    Default Re: QSql Database

    Hi All

    I have used Qsettings instead of QSql..it is woking fine
    What is the difference between Qsql and Qsettings ?
    if i use Qsettings to save Chat history,will saved data in settings load with application?
    where the saved data will be stored ?
    How can i store the Datas in file and read from file using Qsql ?

    can anybody provide me a example


    Thanks

    Yuvaraj R

  6. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSql Database

    Quote Originally Posted by addu View Post
    Hi All

    I have used Qsettings instead of QSql..it is woking fine
    What is the difference between Qsql and Qsettings ?
    if i use Qsettings to save Chat history,will saved data in settings load with application?
    where the saved data will be stored ?
    How can i store the Datas in file and read from file using Qsql ?

    can anybody provide me a example


    Thanks

    Yuvaraj R
    READ THE MANUAL

  7. #7
    Join Date
    May 2009
    Posts
    129

    Default Re: QSql Database

    I read the manual

    And one more thing ..

    How do we store the List using Qsettings

    Thanks

    Yuvaraj R

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. cannot share the database connection!!!!
    By cbarmpar in forum Qt Programming
    Replies: 13
    Last Post: 23rd September 2008, 14:42
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 14:31
  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.