2

I'm relatively new to Qt but I have just finished my first program for a project. I have a .sqlite file that I open by doing this:

 db = QSqlDatabase::addDatabase("QSQLITE");
 db.setDatabaseName("/Users/.../.../Code/Database/AIOCS.sqlite"); //shortened for clarity

I need to send this program to my professor, but obviously if the program tries to compile, the database won't open because the path is wrong. Im trying to run the application without having to specify the file path in my the code. Is there a way to place the file in a local directory where I only have to specify the file name. Something like:

db.setDatabaseName("AIOCS.sqlite");

For example, if I run my program on a mac, I will have to use:

db.setDatabaseName("/Users/.../.../Code/Database/AIOCS.sqlite"); //shortened for clarity

but, on pc it will be

db.setDatabaseName("C:\\User\\...\\Code\\Database\\AIOCS.sqlite"); //shortened for clarity

Is there a way to just use the file name, that way the program can run on any machine without changing code.

3
  • The question is really about file paths, it doesn't have anything specifically to do with SQLite or the Qt Creator IDE. Could you edit the question to be more specific? Commented Nov 28, 2015 at 2:59
  • I tried to be more specific, please let me know if I can provide any more information Commented Nov 28, 2015 at 5:23
  • Use relative filpaths, e.g. "db.setDatabaseName("../../Code/Database/AIOCS.sqlite");" Commented Nov 29, 2015 at 19:18

1 Answer 1

1

You could use Qt OS specific macros to solve this problem. For example,

#if defined(Q_OS_MAC)
    QString prefix = "/Users/.../.../Code/Database/";
#elif defined(Q_OS_WIN)
    QString prefix = "C:\\User\\...\\Code\\Database\\";
#endif

db.setDatabaseName(prefix + "AIOCS.sqlite");

Then you don't need to change the values when you compile the source on Win/Mac machines.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow thats pretty cool. I didn't know you could do that. What I ended up doing was creating a readme that instructed to place the database file onto the desktop and then I used db.setDatabaseName(QDir::homePath()+"/Desktop/AIOCS.sqlite")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.