Results 1 to 20 of 25

Thread: Releasing database file with QSqlDatabase

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


  2. #2
    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
  •  
Qt is a trademark of The Qt Company.