Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25

Thread: Releasing database file with QSqlDatabase

  1. #21
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Releasing database file with QSqlDatabase

    Thanks wysota but, I don't understand what you say with "in frist place".
    I'd like to open only one connection per thread, so the thread can create and manage one DB, and when the test end, it should close the database, zip the file and store it into a MYSQL db archive, and delete the original db file and the zip file.

    This is the code of my thread class; the Header:
    Qt Code:
    1. #ifndef MYTHREAD1_H
    2. #define MYTHREAD1_H
    3.  
    4. #include <QThread>
    5. #include <QDataStream>
    6. #include <QTextStream>
    7. #include <QMutex>
    8. #include <QReadWriteLock>
    9. #include <QSettings>
    10. #include <QMessageBox>
    11. #include <QSqlDatabase>
    12. #include <QSqlQuery>
    13. #include <QSqlError>
    14.  
    15. #include <QDir>
    16. #include <QFile>
    17. #include "quazip/quazip.h"
    18. #include "quazip/quazipfile.h"
    19.  
    20. class myThread1 : public QThread
    21. {
    22. Q_OBJECT
    23.  
    24. public:
    25. myThread1(int nrampa, int sleep);
    26. void stop();
    27.  
    28. public slots:
    29. void generateID();
    30. void execCmd(QString cmd);
    31. void clearDBConn();
    32. void zipDB();
    33.  
    34. protected:
    35. void run();
    36.  
    37. private:
    38. volatile bool stopped;
    39. QMutex mutex;
    40. QString prefix;
    41. int sleep,nrampa,idProva;
    42. QSettings *set;
    43. QString nomeDB;
    44. QSqlQuery *qry;
    45.  
    46. void createDB();
    47. bool extract(const QString & filePath, const QString & extDirPath, const QString & singleFileName);
    48. bool archive(const QString & filePath, const QString & path, bool isFile, const QString & comment=QString(""));
    49.  
    50.  
    51. signals:
    52. void newId(int rampa, QString newId);
    53. void zipFileName(int idProva,QString zipName);
    54.  
    55.  
    56. };
    57.  
    58. #endif // MYTHREAD1_H
    To copy to clipboard, switch view to plain text mode 

    and the CODE:
    Qt Code:
    1. #include "mythread1.h"
    2.  
    3. myThread1::myThread1(int nrampa, int sleep)
    4. {
    5. stopped=true;
    6. [...]
    7. }
    8.  
    9. void myThread1::run(){
    10. // stopped=false;
    11. // while(!stopped){
    12. // //int val = qrand();
    13. // if (genID){
    14.  
    15. // }
    16. // msleep(sleep);
    17. // }
    18. }
    19.  
    20. bool myThread1::extract(const QString & filePath, const QString & extDirPath, const QString & singleFileName) {
    21.  
    22. [...]
    23.  
    24. }
    25.  
    26. bool myThread1::archive(const QString & filePath, const QString & path, bool isFile, const QString & comment) {
    27. [...]
    28. }
    29.  
    30.  
    31. void myThread1::stop(){
    32. stopped=true;
    33. }
    34.  
    35. void myThread1::generateID(){
    36. [...]
    37.  
    38. }
    39.  
    40. void myThread1::createDB(){
    41. nomeDB=QString("R%2_Prova%1.s3db").arg(idProva).arg(nrampa);
    42. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",nomeDB);
    43. db.setDatabaseName(nomeDB);
    44. if (!db.open()){
    45. msg.setText(db.lastError().text());
    46. msg.show();
    47. }else{
    48. db.exec("create table test (campo int)");
    49. }
    50. }
    51.  
    52. void myThread1::execCmd(QString cmd){
    53. qry = new QSqlQuery(db);
    54. qry->exec(cmd);
    55. }
    56.  
    57. void myThread1::clearDBConn(){
    58. qry->finish();
    59. db.close();
    60. db.~QSqlDatabase();
    61. QSqlDatabase::removeDatabase(nomeDB);
    62. }
    63.  
    64. void myThread1::zipDB(){
    65. QString zipName,delName;
    66. zipName=nomeDB;
    67. clearDBConn();
    68. archive(zipName.replace(".s3db",".zip"),nomeDB,true);
    69. emit zipFileName(idProva, zipName);
    70. QFile(nomeDB).remove();
    71. }
    To copy to clipboard, switch view to plain text mode 

    If I understood your suggest, I should use
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    isn't it? So the db is a local object and at the end of the method it free the heap, isn't it?
    But so, I should create one connection for every time I call the execCmd method, is right?

    No other way to close and re-open the db connection?

    Thanks for your time

  2. #22
    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: Releasing database file with QSqlDatabase

    Quote Originally Posted by cia.michele View Post
    Thanks wysota but, I don't understand what you say with "in frist place".
    It's a figure of speech. I meant to ask why are you storing the database object (db) at all. QSqlDatabase objects are not meant to be stored as class member variables. At any point when you need the db object, you can retrieve it using QSqlDatabase::database() passing it the connection name passed to addDatabase() or cloneDatabase().
    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.


  3. #23
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Releasing database file with QSqlDatabase

    Thanks wysota, I trying to do what you say, but I think didn't understand so much.
    I try to do it in this way, is the correct way you sayd?

    Qt Code:
    1. void myThread1::createDB(){
    2. nomeDB=QString("R%2_Prova%1.s3db").arg(idProva).arg(nrampa);
    3. QSqlDatabase::addDatabase("QSQLITE",nomeDB);
    4. QSqlDatabase(nomeDB).setDatabaseName(nomeDB);
    5. if (!QSqlDatabase(nomeDB).open()){
    6. msg.setText(QSqlDatabase(nomeDB).lastError().text());
    7. msg.show();
    8. }else{
    9. QSqlDatabase(nomeDB).exec("create table test (campo int)");
    10. }
    11. }
    12.  
    13. void myThread1::execCmd(QString cmd){
    14.  
    15. if (!QSqlDatabase(nomeDB).open()){
    16. msg.setText(QSqlDatabase(nomeDB).lastError().text());
    17. msg.show();
    18. }else{
    19. QSqlDatabase(nomeDB).exec(cmd);
    20. }
    21. QSqlDatabase(nomeDB).close();
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 
    I got this error:
    'QSqlDatabase::QSqlDatabase(const QString&)' is protected
    In which way I could address the database passing the connection name?

    Thanks for your time

    Michele

  4. #24
    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: Releasing database file with QSqlDatabase

    QSqlDatabase::database() retrieves a handler to the database.
    Qt Code:
    1. QSqlDatabase::addDatabase("QSQLITE", nomeDB);
    2. {
    3. // ...
    4. QSqlDatabase db = QSqlDatabase::database(nomeDB); // a local variable, not a class member
    5. db.exec(cmd);
    6. db.close();
    7. }
    8. QSqlDatabase::removeDatabase(nomeDB);
    To copy to clipboard, switch view to plain text mode 
    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.


  5. #25
    Join Date
    Oct 2009
    Posts
    65
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Releasing database file with QSqlDatabase

    Thanks a lot wysota!
    Now it's RUN!

    This is the definitive code

    Qt Code:
    1. void myThread1::createDB(){
    2. nomeDB=QString("R%2_Prova%1.s3db").arg(idProva).arg(nrampa);
    3. QSqlDatabase db= QSqlDatabase::addDatabase("QSQLITE",nomeDB);
    4. db.setDatabaseName(nomeDB);
    5. if (!db.open()){
    6. msg.setText(db.lastError().text());
    7. msg.show();
    8. }else{
    9. db.exec("create table test (campo int)");
    10. }
    11. }
    12.  
    13. void myThread1::execCmd(QString cmd){
    14.  
    15. QSqlDatabase::database(nomeDB).exec(cmd);
    16.  
    17. }
    18.  
    19. void myThread1::zipDB(){
    20. QString zipName;
    21. zipName=nomeDB;
    22. QSqlDatabase::removeDatabase(nomeDB);
    23. archive(zipName.replace(".s3db",".zip"),nomeDB,true);
    24. emit zipFileName(idProva, zipName);
    25. QFile(nomeDB).remove();
    26. }
    To copy to clipboard, switch view to plain text mode 

    Now I use the QSqlDatabase db object only for set the database name, as variable and not as class member.

    It run great.

    Thanks

    Michele

Similar Threads

  1. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. Database: How to use an external file?
    By goes2bob in forum Newbie
    Replies: 10
    Last Post: 23rd January 2008, 14:07
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

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.