How to replace SQLite ".db3" file with the new one?
-
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.
-
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.
-
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? -
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".
-
@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? -
@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?@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]
-
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
-
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.
-
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.