Qt World Summit: Register Today!

SQL DB file migration and path from desktop to android kit

  • Hi,

    I created an app that reads from a previously made .db file. The app works perfect when run on desktop environment. I have entered the complete address of db file in codes

    events_db = QSqlDatabase::addDatabase("QSQLITE");

    But there is a problem when i make android app from it. The app successfully deploys into the device but

    the problem is the android app cannot find the db file with it. I am not even sure wether the db file is copied from desktop project folder to the build apk.

    So my questions are

    1. How to make sure the db or other important files that are used in qt programme goes from desktop to android kit.
    2. How to use a global path in QT by which it works effectively in desktop and android kits.

    Any example would be great.

    Thank You

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What kind of files are you referring to ?
    Why do you want to copy these files from desktop to mobile.

    In any case, take a look at QStandardPaths.

  • Hi thank you for the reply. I am using a sql db file. I am using that file to read the data. The data is filled by some another application. Initially to keep it simple i put the sql db file in same folder where my project other files are.

    But in the coding I am giving a complete address. So for example in computer it is in "xyz" folder in c drive . This programme works as when i am giving complete address the program knows as it is in running on computer.

    But the problem arises when i change this program to android amrv7 architecture than my android device attached with my computer runs the same application . However, It cannot read the file because that path does not exists.

    So as you mentioned QstandardPaths I will try this but I am worried in the first step as when i change my desktop application to android application does all the files provided in the project folder are copied to android project folder. I am asking this because I was not able to find sql db file in android project folder as qt makes a different folder for android v7 during building it.

    Or I can add sql db file in resources. But as far as i remember it was also not a good option as described in https://forum.qt.io/topic/14714/adding-db-to-resources/9.

    Thank You for your support.

  • Update

    Quick Solution / cheap fix

    I can do that by manually copying the sql db file to a preffered location in android and then writing complete path for that location in qt codes.

    Obviously this thing works but it does not make me use of a global path for the code solution of different devices / architectures i.e. it itself redirect to its project folder location irrespective of kit its using . Basically what I am doing at the moment is - changing the path of sql db manually for different kits/architectures. I am also not able to find any option for copying (in an automate style) the sql db file from desktop to android. Like Qt is doing for images via resources.

    If any such thing exists please let me know

    Thank You

  • Lifetime Qt Champion

    You do realize that you are trying to access the same database file at the same time from two different devices ?
    There's something fishy about your architecture.

    What exactly is the application on the Android device supposed to do ?

  • @SGaist No, the database file is created before. So at the time when android is using it . It is not used by any other application. The project is done for demo purpose so architecture is not a problem.

    However, we are moving away from the base of the question. Andrroid application is suppose to read the data from sql db file and display the content on the screen.

    As I mentioned that it can be achieved via "quick fix / cheap solution" (mentioned above). I am looking for a better approach.

  • Lifetime Qt Champion

    Ok, then it would have been better to explain that from the start.

    What size is your database ? If not too big, you can put it in Qt's resources system and then copy that file from the resource to a suitable location given by QStandardPaths. You may have to create the folder given before copying the database file over there.

Log in to reply