Database location is not working if it is taken from external source
-
I have a simple database in location "c:/data/a.db"
It is opened and used in program using
db.open("c:/data/a.db");What I want is to store this location in file and take location from there and execute the process
QString databaseLocation = readTextFile(QString path); /* the path is also valid and readTextFile("./location.txt") is also returning correct value */ QString readTextFile(QString path) { QFile file(path); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&file); return in.readAll(); } return ""; } // here databaseLocation = "c:/data/a.db"; which is same as of top db.open(databaseLocation); //this is not workingSo the location given inside open is working and text given there is not working .
What is wrong with this
Please check thisThank You
-
I have a simple database in location "c:/data/a.db"
It is opened and used in program using
db.open("c:/data/a.db");What I want is to store this location in file and take location from there and execute the process
QString databaseLocation = readTextFile(QString path); /* the path is also valid and readTextFile("./location.txt") is also returning correct value */ QString readTextFile(QString path) { QFile file(path); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&file); return in.readAll(); } return ""; } // here databaseLocation = "c:/data/a.db"; which is same as of top db.open(databaseLocation); //this is not workingSo the location given inside open is working and text given there is not working .
What is wrong with this
Please check thisThank You
@Thank-You
This is not possible. If you need exactly the string"c:/data/a.db", it does not matter whether you hard-code it or read it from a file. If that is what you are saying, there is something wrong. Unless I am misunderstanding what you are saying....Purely at a guess: when you read it from file, does the line have a newline at the end? Which maybe you are then including at the end of the string passed to
db.open(), which probably won't then work.Look more closely at exactly what you are getting/passing as the string read from the file. I would not expect
in.readAll()to read a line from a file; more likein.readLine():The returned line has no trailing end-of-line characters ("\n" or "\r\n"), so calling QString::trimmed() can be unnecessary.
[NOTE @Christian-Ehrlicher has made a post. I read what you are saying is happening/the problem very differently from his interpretation of what you have written.]
You should look at storing any "setting" like this in
QSettingsclass, not in a file of your own. -
Because the path is relative (to the current working directory). You either look for QCoreApplication::applicationDirPath() or better QStandardPaths::writeableLocation()
-
I have a simple database in location "c:/data/a.db"
It is opened and used in program using
db.open("c:/data/a.db");What I want is to store this location in file and take location from there and execute the process
QString databaseLocation = readTextFile(QString path); /* the path is also valid and readTextFile("./location.txt") is also returning correct value */ QString readTextFile(QString path) { QFile file(path); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { QTextStream in(&file); return in.readAll(); } return ""; } // here databaseLocation = "c:/data/a.db"; which is same as of top db.open(databaseLocation); //this is not workingSo the location given inside open is working and text given there is not working .
What is wrong with this
Please check thisThank You
@Thank-You
This is not possible. If you need exactly the string"c:/data/a.db", it does not matter whether you hard-code it or read it from a file. If that is what you are saying, there is something wrong. Unless I am misunderstanding what you are saying....Purely at a guess: when you read it from file, does the line have a newline at the end? Which maybe you are then including at the end of the string passed to
db.open(), which probably won't then work.Look more closely at exactly what you are getting/passing as the string read from the file. I would not expect
in.readAll()to read a line from a file; more likein.readLine():The returned line has no trailing end-of-line characters ("\n" or "\r\n"), so calling QString::trimmed() can be unnecessary.
[NOTE @Christian-Ehrlicher has made a post. I read what you are saying is happening/the problem very differently from his interpretation of what you have written.]
You should look at storing any "setting" like this in
QSettingsclass, not in a file of your own. -
@Thank-You
This is not possible. If you need exactly the string"c:/data/a.db", it does not matter whether you hard-code it or read it from a file. If that is what you are saying, there is something wrong. Unless I am misunderstanding what you are saying....Purely at a guess: when you read it from file, does the line have a newline at the end? Which maybe you are then including at the end of the string passed to
db.open(), which probably won't then work.Look more closely at exactly what you are getting/passing as the string read from the file. I would not expect
in.readAll()to read a line from a file; more likein.readLine():The returned line has no trailing end-of-line characters ("\n" or "\r\n"), so calling QString::trimmed() can be unnecessary.
[NOTE @Christian-Ehrlicher has made a post. I read what you are saying is happening/the problem very differently from his interpretation of what you have written.]
You should look at storing any "setting" like this in
QSettingsclass, not in a file of your own.