Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    SQLITE, I delete que sqlite file and the database still loading data from the file...

    General and Desktop
    4
    15
    3229
    Loading More Posts
    • 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.
    • D
      dcbasso last edited by

      HI...
      Me Again!
      People, I put my sqlite in "C:\ProgramData\MyApp\database.sqlite" and works fine on my app.
      Now I remove the file, delete and start the app and the app still showing the data that was in the file!
      I try to reboot the virtual machine remove the folder and the app still showing and querying data from my file that in not there anymore!

      Why that?
      @
      static QSqlDatabase getDatabase()
      {
      QSqlDatabase database = QSqlDatabase::database(getConnectionName());
      if (! database.isValid())
      {
      database = QSqlDatabase::addDatabase(DRIVER, getConnectionName());
      database.setDatabaseName( getDataBaseName() );
      }
      isDataBaseCreated();
      return database;
      }
      @
      @
      static QString getDataBaseName()
      {
      QString path;
      QSettings* regDatabase = new QSettings(REG_APP, QSettings::NativeFormat);
      path = regDatabase->value(REG_DATABASE, QSettings::NativeFormat).toString();
      return path;
      }
      @
      @
      static QString getConnectionName()
      {
      return "mimo_database";
      }
      @
      Please any tip or help?

      1 Reply Last reply Reply Quote 0
      • D
        dcbasso last edited by

        I have a older project, very similar use of sqlite and I don't this problem!
        Before I use Qt 4.8 and now I'm using 5.2!

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Are you sure you are removing the correct file ? What does getDataBaseName return ?

          Also, you have a memory leak in getDataBaseName(), you don't need to create the QSettings object on the heap. If you still want to do it, delete before the return statement.

          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 Reply Quote 0
          • D
            dcbasso last edited by

            The getDataBaseName return this: “C:\ProgramData\MyApp\database.sqlite"!
            I will delete, but this is not the reason of my problem... right?

            1 Reply Last reply Reply Quote 0
            • C
              clochydd last edited by

              Hi, you had already deleted that file, didn't you?
              So deleting again should bring a similar result.

              I suggest you search for files "database.sqlite" on your machine and - if there is any - rename it and restart your program.
              If not, you should try to get info of the database in debug mode in your app when it shows the data from the file.

              1 Reply Last reply Reply Quote 0
              • D
                dcbasso last edited by

                Well... I try rename the the database on disk, and still loading then!

                I try this: change the return of "getDataBaseName()" to “C:\ProgramData\MyApp\database.sqlite123”, and I got no database because there is no database with this name...
                I really can't understand that... I reading some docs and examples to see the problem...

                1 Reply Last reply Reply Quote 0
                • C
                  clochydd last edited by

                  Can you collect more information by using
                  @
                  qDebug() << database.databaseName();
                  qDebug() << database.hostName()
                  @

                  I presume your database is in memory - see "In-Memory SQLite":https://www.sqlite.org/inmemorydb.html

                  1 Reply Last reply Reply Quote 0
                  • D
                    dcbasso last edited by

                    Well... I as thinking about in memory sqlite, but the file is updated... I open in FireFox addon "Sqlite Manager" and query stuff and works well!

                    1 Reply Last reply Reply Quote 0
                    • D
                      dcbasso last edited by

                      Result:
                      @
                      qDebug() << database.databaseName();
                      qDebug() << database.hostName();
                      @

                      "C:\ProgramData\MyApp\database.sqlite"
                      ""

                      Another very interesting thing is, the return of this code is TRUE:

                      @
                      static bool isDataBaseCreated()
                      {
                      QFile* arquivo = new QFile(getDataBaseName());
                      bool retorno = arquivo->exists();
                      qDebug() << "File name: " << getDataBaseName();
                      qDebug() << "File exists: " << retorno;
                      arquivo->close();
                      delete arquivo;
                      return retorno;
                      }

                      File name: "C:\ProgramData\MyApp\database.sqlite"
                      File exists: true
                      @

                      The problem is... the file is really not there!!!

                      1 Reply Last reply Reply Quote 0
                      • C
                        clochydd last edited by

                        I do not work with Windows very often so I hope its not a silly question: Can a hidden file of that name exist?

                        1 Reply Last reply Reply Quote 0
                        • D
                          dcbasso last edited by

                          Well check it and nothing there!
                          I always use Linux, but in this project I make everything on windows...
                          I'm change my way to connect the database right now... maybe I can solve that...

                          1 Reply Last reply Reply Quote 0
                          • D
                            dcbasso last edited by

                            Well...
                            I try and Try... and...
                            Appears some kind of cache, I not really sure or maybe some bug on Qt 5.2...
                            I really don't know what do to anymore...

                            1 Reply Last reply Reply Quote 0
                            • A
                              andreyc last edited by

                              Did you try to check if file exists in command shell (cmd.exe)?
                              An explorer may hide some files.

                              Run the following commands in cmd.exe
                              @
                              dir C:\ProgramData\MyApp\database.sqlite
                              dir C:\ProgramData\MyApp*
                              @

                              1 Reply Last reply Reply Quote 0
                              • D
                                dcbasso last edited by

                                well guys...
                                after some suffering I make some crazy code:

                                @
                                static QSqlDatabase getDatabase()
                                {
                                QSqlDatabase database = QSqlDatabase::database(getDataBaseName());
                                qDebug() << "isOpen: " << database.isOpen();
                                qDebug() << "isValid: " << database.isValid();
                                if (! database.isValid())
                                {
                                database = QSqlDatabase::addDatabase(DRIVER, getDataBaseName());
                                database.setHostName(DB_HOSTNAME);
                                database.setDatabaseName(getDataBaseName());
                                database.removeDatabase();
                                isDataBaseCreated(database);

                                    }
                                    qDebug() << "Database host: " << database.hostName();
                                    qDebug() << "Database name: " << database.databaseName();
                                    return database;
                                }
                                

                                @
                                @
                                static bool isDataBaseCreated(QSqlDatabase db)
                                {
                                bool exists = false;
                                QFile* arquivo = new QFile(getDataBaseName());
                                exists = arquivo->exists();
                                if (exists)
                                {
                                arquivo.remove();
                                }
                                delete arquivo;
                                return exists;
                                }
                                @

                                In resume:

                                Using my app, with "same code" and force the file to be deleted and force to database be removed...

                                Now appears to be solve my problem...

                                Question: It's recommend before close app do this:

                                @
                                database.close();
                                database.removeDatabase();
                                @

                                Thanks all!

                                1 Reply Last reply Reply Quote 0
                                • D
                                  dcbasso last edited by

                                  Yes I check, with and without Administrator cmd!

                                  [quote author="andreyc" date="1396027781"]Did you try to check if file exists in command shell (cmd.exe)?
                                  An explorer may hide some files.

                                  Run the following commands in cmd.exe
                                  @
                                  dir C:\ProgramData\MyApp\database.sqlite
                                  dir C:\ProgramData\MyApp*
                                  @[/quote]

                                  1 Reply Last reply Reply Quote 0
                                  • First post
                                    Last post