How to access a file (CAsqlite.db) from a flatpak/python/Builder project?

Projeto python/Gnome construído com Builder em fedora 41. Tenho o arquivo do banco de dados chamado ‘CAsqlite.db’, um arquivo ‘conectar.py’ para realizar a conexão e enfermedad_window.ui para chamar outra janela desde a janela principal.

Tenho a seguinte linha de código para conectar uma base de dados sqlite:

           with sqlite3.connect(database='/com/github/thorhent/CA/CAsqlite.db') as conn:

A minha dúvida é como trabalhar com as rotas ou caminhos aos arquivos extras, como CAsqlite.db. Entendi que o enfermedad_window.ui deve estar adicionada no arquivo gresource.xml do projeto e os arquivos .py dentro de meson.build.

No arquivo window.py, tenho um método para chamar ler enfermedad_window.ui com a seguinte instrução:

       builder.add_from_file('/com/github/thorhent/CA/enfermedad_window.ui')

Se eu coloco a direção literal dos arquivos na pasta do meu projeto (‘/home/user/Projects/clinicalayudante/src/arquivo’), então funciona a partir do gnome-builder, somente. Qual a forma correta de definir os caminhos ou que outras configurações devem ser feitas?

Espero que eu tenha conseguido explicar o meu problema. Obrigado.

Google English Translation:

Python/Gnome project built with Builder on Fedora 41. I have a database file called ‘CAsqlite.db’, a ‘connect.py’ file to make the connection and enfermedad_window.ui to call another window from the main window.

I have the following line of code to connect to a sqlite database:

    with sqlite3.connect(database='/com/github/thorhent/CA/CAsqlite.db') as conn:

My question is how to work with the routes or paths to the extra files, such as CAsqlite.db. I understood that enfermedad_window.ui must be added to the project’s gresource.xml file and the .py files inside meson.build.

In the window.py file, I have a method to call read nursing_window.ui with the following instruction:

  builder.add_from_file('/com/github/thorhent/CA/enfermedad_window.ui')

If I put the literal path of the files in my project folder (‘/home/user/Projects/clinicalayudante/src/file’), then it works from gnome-builder, only. What is the correct way to define the paths or what other configurations should be made?

I hope I was able to explain my problem. Thank you.

Se você entende inglês, consegue traduzir para o inglês?

If you can understand English, can you translate to English?

I have added the English text from a translation with google translator. I hope it helps.

1 Like

Well, the path /com/github/thorhent/CA/ is a virtual one used by GResources. Unless you bundle the database file inside the GResources file, it won’t be available from there.

However, considering this is a database file, I assume you want to store user configuration inside this file?
In this case, you need a different approach anyway.

So, here is how I personally would handle this:
First of all, on the first run, create the database from your application code. Here’s the reasoning for this: You can’t expect the file to exist in the user configuration folder on first launch (obviously), and installation folders are generally read-only. And since you already will have code to read and write the database, it would make sense to have the initial creation there as well.

When your application has created the database, you can the store it in the user data or configuration folder.
You can get the path to these folders with the functions GLib.get_user_data_dir and GLib.get_user_config_dir respectively. In this folder, you create a folder for your app with your app name or app id, and inside there you store the database.

On subsequent launches, you can check if the file exists under this path. If so, load the file from there.

Thank you, I will think of a solution based on your valuable guidance. However, my database is not empty and the application only performs reads.

My database does not require username and password.

Well, if the database is read-only, then its a bit of an different story.

You could add the database to your GResources file and then access it with GFile from the path resource://....
But I’m not sure if this would exceed what GResources were designed for… Normally its only used for UI files and text files and images.

Otherwise you probably want to install the database into the system data directory. Probably by using meson’s install_data to install it into install_prefix/share/appname.
If the install prefix is a well-known system path, like /usr/local, you could then try to find the file from the system data directories accessible with GLib.get_system_data_dirs.

I regret deleting it, it could help others to build their reasoning.

I had to upload the changes to github, because after I rebuilt and installed it with the updated repository, it worked.

Thank you very much.