Results 1 to 4 of 4

Thread: SQL QThread how i can insert a lock?

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default SQL QThread how i can insert a lock?

    I have large zip file to insert on a DB (mysql)

    How i can insert a lock on a way wo the next Thread not insert data on this table?

    or can i solve this if i put the zip packer on the same Thread ... as this

    If the user create fast 3 or more page?


    Qt Code:
    1. /* max bit upload to one page cms zip #define MAXUPLOADFOLDER 2563243 2.44 MB mysql long blob */
    2. class PushQueryZip : public QThread, public Base_Modul
    3. {
    4. Q_OBJECT
    5. public:
    6. void SetupQuery( QSqlDatabase db , uint idmg , const QString filego)
    7. {
    8. LoopNummer = 0;
    9. loopi = true;
    10. dbconnection = db;
    11. having = false;
    12. writteln = filego;
    13. pagenummer = idmg;
    14.  
    15. }
    16. void run() {
    17.  
    18. QByteArray inside;
    19. QFile f(writteln);
    20. bool accessf = f.open(QIODevice::ReadOnly);
    21.  
    22. if (!accessf) {
    23. emit ErrorMsgZip(tr("Unable to open zip."));
    24. exit();
    25. }
    26.  
    27. inside = f.readAll();
    28. f.close();
    29. f.remove();
    30. const QString blobzip = inside.toBase64()+"|end_stream|";
    31. sqlmake = QString("REPLACE INTO PAGEFILE VALUES (%1, 'zip', 'attach' ,'%2',%3)")
    32. .arg(pagenummer)
    33. .arg(blobzip)
    34. .arg(QTime_Null());
    35.  
    36. QSqlQuery query("",dbconnection);
    37. bool success = query.exec(sqlmake);
    38. if (!success) {
    39. emit ErrorMsgZip(tr("Unable to connect and exec query..."));
    40. exit();
    41. }
    42. emit OkJobEnd(pagenummer);
    43. exit();
    44. }
    45.  
    46. signals:
    47. void ErrorMsgZip(QString);
    48. void OkJobEnd(uint);
    49. private:
    50. QSqlDatabase dbconnection;
    51. QString sqlmake;
    52. QString writteln; /* file to go zip ensure dir exist */
    53. QString inside;
    54. int LoopNummer;
    55. uint pagenummer;
    56. bool loopi;
    57. bool having;
    58. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2007
    Posts
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL QThread how i can insert a lock?

    You can use a QMutex. Just make is static to your file ei

    static QMutext mutex;

    Then lock it before calling exec and unlock it once it is done.

    If it's early eough in the cycle of the project to change your table, you can make the field a LARGEBLOB and then suck the file into a QByteArray and just insert/update the data like you would an int or a varchar value. This has the advantage of using standard SQL statements and can be used across a couple of differenttypes of databases.

  3. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: SQL QThread how i can insert a lock?

    Quote Originally Posted by croftj View Post
    You can use a QMutex. Just make is static to your file ei
    static QMutext mutex;
    Then lock it before calling exec and unlock it once it is done.
    If it's early eough in the cycle of the project to change your table, you can make the field a LARGEBLOB and then suck the file into a QByteArray and just insert/update the data like you would an int or a varchar value. This has the advantage of using standard SQL statements and can be used across a couple of differenttypes of databases.
    but how run QMutext it create a file ... or only survey process?

    my target is to save all open page on this cms whit only one button click.. grab each cache dir (xml.image,attachment,ecc) zip and insert on this QThread...

    I observe on send to mysql (same server) need more time as a http PUT Method (3x faster) upload method. the same zip...

    MYSQL dont have LARGEBLOB i found only LongBlob and can save 2GB

    but i stop the folder on a dirmodel by 2.44 MB ... max mysql packet incomming by server setting.

    Qt Code:
    1. CREATE TABLE `PAGEFILE` (
    2. `ID` int(32) NOT NULL default '0',
    3. `TIPO` enum('zip','pdf','txt','doc') default NULL,
    4. `TITEL` varchar(255) default NULL,
    5. `PIC` longblob NOT NULL,
    6. `EPOCHE` int(32) NOT NULL default '0',
    7. PRIMARY KEY (`ID`)
    8. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2006
    Posts
    42
    Thanks
    1
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SQL QThread how i can insert a lock?

    This might not be related to your question, but where do you create the QSqlDatabase object that you pass to the SetupQuery() function?

    The reason I ask is because it looks like you don't create the connection in the same thread as the one which is using it. This is not safe and may cause problems.

    Threads and the SQL Module:
    http://doc.trolltech.com/4.3/threads...the-sql-module

    "A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported."

Similar Threads

  1. Model/View -- Sql Insert
    By kroenecker in forum Qt Programming
    Replies: 3
    Last Post: 3rd May 2007, 15:55

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.