Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to replace SQLite ".db3" file with the new one?
Forum Updated to NodeBB v4.3 + New Features

How to replace SQLite ".db3" file with the new one?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 3.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sinbad
    wrote on 27 Mar 2017, 09:20 last edited by
    #1
    const QString dbName(QApplication::applicationDirPath()+QString("/myDatabase.db3"));
    db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName(dbName);
    db.open();
    

    For the first time file "myDatabase.db3" opens rigth. But when I replace it with the new one, I see tables from the previous (replaced) file.

    I cleared cache from this directory:

    QStandardPaths::locate(QStandardPaths::DataLocation, QString(), QStandardPaths::LocateDirectory);
    

    but it doesn't help me.

    J 1 Reply Last reply 27 Mar 2017, 09:25
    0
    • S Sinbad
      27 Mar 2017, 09:20
      const QString dbName(QApplication::applicationDirPath()+QString("/myDatabase.db3"));
      db = QSqlDatabase::addDatabase( "QSQLITE" );
      db.setDatabaseName(dbName);
      db.open();
      

      For the first time file "myDatabase.db3" opens rigth. But when I replace it with the new one, I see tables from the previous (replaced) file.

      I cleared cache from this directory:

      QStandardPaths::locate(QStandardPaths::DataLocation, QString(), QStandardPaths::LocateDirectory);
      

      but it doesn't help me.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 27 Mar 2017, 09:25 last edited by
      #2

      @Sinbad

      hard to say, you should show us how you try to open your other db and how you "clean up" the old setup.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sinbad
        wrote on 27 Mar 2017, 10:21 last edited by
        #3

        I store my data base in "myDatabase.db3" which is placed in the application directory.

        const QString dbName(QApplication::applicationDirPath()+QString("/myDatabase.db3"));
        db = QSqlDatabase::addDatabase( "QSQLITE" );
        db.setDatabaseName(dbName);
        db.open();
        

        "how you "clean up" the old setup"
        That is my question. What should I do, to correctly replace "myDatabase.db3" file with the new one with the same name, but the new data? How to cleanup cache, setup or smth else?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 27 Mar 2017, 10:33 last edited by
          #4

          Hi,

          You're currently not replacing anything. SQLite either opens an existing database or create a new one if the file doesn't exist. In your case if you want a brand new database each time you start your application then delete the old one first. If you don't need that database to be persistent at all then just create it "in memory".

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • S Offline
            S Offline
            Sinbad
            wrote on 27 Mar 2017, 11:49 last edited by
            #5

            @SGaist said in How to replace SQLite ".db3" file with the new one?:

            You're currently not replacing anything.

            In the code below yes. I replace it using Windows Explorer.

            When I changed the name of the file (or path), everything became good.
            So the question is where can i find the cache and delete it?

            J 1 Reply Last reply 27 Mar 2017, 12:01
            0
            • S Sinbad
              27 Mar 2017, 11:49

              @SGaist said in How to replace SQLite ".db3" file with the new one?:

              You're currently not replacing anything.

              In the code below yes. I replace it using Windows Explorer.

              When I changed the name of the file (or path), everything became good.
              So the question is where can i find the cache and delete it?

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 27 Mar 2017, 12:01 last edited by SGaist
              #6

              @Sinbad

              @SGaist said in How to replace SQLite ".db3" file with the new one?:

              In your case if you want a brand new database each time you start your application then delete the old one first. If you don't need that database to be persistent at all then just create it "in memory".

              const QString dbName(QApplication::applicationDirPath()+QString("/myDatabase.db3"));
              QFile file(dbName);
              if(file.exits())
                 qDebug() << "Debug output to see if remove was succesful or not" << file.remove();
              db = QSqlDatabase::addDatabase( "QSQLITE" );
              db.setDatabaseName(dbName);
              db.open();
              

              [edit: fixed missing parenthesis SGaist]


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 27 Mar 2017, 12:01 last edited by
                #7

                You are setting your database at QApplication::applicationDirPath()+QString("/myDatabase.db3") so it's that one that will be created if the file does not exist and in the same idea, it will be the one opened if the file does exist.

                So like I wrote before, if you want to re-create a new database file each time your application start, delete it before calling db.setDatabaseName

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                3
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 27 Mar 2017, 12:15 last edited by
                  #8

                  if memory permits then opening it as
                  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                  db.setDatabaseName(":memory:");
                  Will never save anything in first place. :)
                  as @SGaist explained.

                  1 Reply Last reply
                  3
                  • D Offline
                    D Offline
                    DanielVanRhyn
                    wrote on 29 Sept 2017, 13:05 last edited by
                    #9

                    Windows has a VirtualStore function where if the program tries to write to a unwritable file (based on permissions, e.g. Program Files), it'll copy that file into USER/AppData/Local/VirtualStore/Program Files/.... This new file is not deleted when the program is uninstalled, but the QT application will see it as residing at its original location. This is why you see data from the old DB after you replaced it.

                    A solution is to open the Sqlite database in read only mode.

                    db = QSqlDatabase::addDatabase( "QSQLITE" );
                    database_.setConnectOptions(QLatin1String("QSQLITE_OPEN_READONLY"));
                    db.setDatabaseName(dbName);
                    db.open();
                    

                    If you require write access, either run your application as "Administrator" or install it in a location you have write access to.

                    1 Reply Last reply
                    1
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 29 Sept 2017, 19:38 last edited by
                      #10

                      I wouldn't recommend running an application as Admin in order for it to create a file in a folder that's read-only on purpose and in this case that would then be readable and read by every user of the station.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved