Joaquin
Joaquin

Reputation: 23

How can I create a python exe file with a sqlite database incorporated?

I've programed a code in python that uses a sqlite database. Now i need to send an .exe file to other people and they have to run it properly only using the .exe file and without the .db files. But i don't know how to create an executable that includes the sqlite database. Any help?

Upvotes: 0

Views: 3467

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148910

You can use installers to convert a Python script into a Windows .exe file. Most of them are able to add files to the bundle.

So with PyInstaller, you could:

  • at build time:

    • dump the sqlite database (.db file) to a sql script
    • add the script to the PyInstaller bundle in the data sub-field of Analysys in the spec file
    • convert everything to a single executable
  • at run time:

    • the PyInstaller loader will extract the bundle into a temporary folder

    • the python code can get the path of that folder in sys._MEIPASS

    • from there on, it knows the path of the dump and can load it into a in-memory database:

        import sqlite3
        ...
        con = sqlite3.connect(':memory:')
        con.executescript(open(path_to_dump_file).read())
      
    • con now gives access to the content of the database...

This only gives hints on the how-to, but a detailed recipe would really depend on the installer and on your applicaiton.

Upvotes: 1

Related Questions