Search code examples
pythonsqlitecx-freeze

Create and update SQLite db from python .exe-file (using cx_Freeze)


Greetings! This is my first question here, after having used the forum for several years. I will do my best to follow the guidelines, please comment with improvement suggestions to my question.


My program gathers info from the steamspy.com API in JSON strings. These are written into a SQLite database.

The program runs with no problems directly from the .py file, however, I would like to be able to run and distribute my program as a single executable file.

I havse used cx_Freeze to create an .exe file, but the program does not generate the SQLite database. I have searched extensively throughout this forum and the web for a solution, without any luck.

Below I have pasted the code for my main program steamSpy.py:

input("Press Enter to continue...")

import requests
import time
import sys
import sqlite3
import datetime

unix=time.time()
date = str(datetime.datetime.fromtimestamp(unix).strftime('%y-%m-%d'))
conn = sqlite3.connect('steamDB.db') #Starts connection
c = conn.cursor() #Starts cursor

def create_table():
    c.execute('Create TABLE IF NOT EXISTS steamData(counter REAL, date REAL, name TEXT, developer TEXT, score_rank REAL, owners REAL, owners_variance REAL, players_forever REAL, players_forever_variance REAL, players_2weeks REAL, players_2weeks_variance REAL, average_forever REAL, average_2weeks REAL, median_forever REAL, median_2weeks REAL, ccu REAL, price REAL)')

def dynamic_data_entry():
#    score_rank = []
#    owners = []
    c.execute("INSERT INTO steamData (counter, date, name, developer, score_rank , owners , owners_variance , players_forever , players_forever_variance , players_2weeks , players_2weeks_variance , average_forever , average_2weeks , median_forever , median_2weeks , ccu , price) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
              (counter, date, str(name)[2:-2], str(developer)[2:-2], str(score_rank)[1:-1], str(owners)[1:-1], str(owners_variance)[1:-1], str(players_forever)[1:-1], str(players_forever_variance)[1:-1], str(players_2weeks)[1:-1], str(players_2weeks_variance)[1:-1], str(average_forever)[1:-1], str(average_2weeks)[1:-1], str(median_forever)[1:-1], str(median_2weeks)[1:-1], str(ccu)[1:-1], str(price)[2:-2]))
    conn.commit()

create_table()

def read_from_db():
    c.execute("SELECT counter, date FROM steamData WHERE counter=1") #cursor is populated now * but nothing has happened
    data_read = c.fetchall()
    if any(date in s for s in data_read):
        c.close() #Ends connection
        conn.close()
        sys.exit('Caution! Data from current date is already present in the database... Ending process')

#Check for updated data
read_from_db()

#Get data
baseURL = "http://steamspy.com/api.php?request="
request = "appdetails&appid="
codes = ["570", "518790", "322920", "253710"]
url = codes
counter = 0
for x in url: 
    counter = counter + 1
    response = requests.get(baseURL + request + x)
    data = response.json()
    name = [data["name"]]
    developer = [data["developer"]]
    score_rank = [data["score_rank"]]
    owners = [data["owners"]]
    owners_variance = [data["owners_variance"]]
    players_forever = [data["players_forever"]]
    players_forever_variance = [data["players_forever_variance"]]
    players_2weeks = [data["players_2weeks"]]
    players_2weeks_variance = [data["players_2weeks_variance"]]
    average_forever = [data["average_forever"]]
    average_2weeks = [data["average_2weeks"]]
    median_forever = [data["median_forever"]]
    median_2weeks = [data["median_2weeks"]]
    ccu = [data["ccu"]]
    price = [data["price"]]
    
    #Write to database
    dynamic_data_entry()
    time.sleep(0.25)

#Close connection
c.close() #Ends connection
conn.close() 


print("Program finished.")

Next up is my code for the setup.py file:

from cx_Freeze import setup, Executable

setup(name='steamSpy',
      version='0.2',
      description='Gather_data',
      executables=[Executable("steamSpy.py")])

This is most likely trivial info, but in case someone is in doubt, the executable can be created with one of the following lines in the command prompt:

python setup.py build

python setup.py bdist_msi


Solution

  • The setup.py should include all the modules used in your python program. And sqlite3 requires a specific .dll file in addition.

    Try this setup.py, it should work.

    from cx_Freeze import setup, Executable
    import sys
    import os
    
    build_exe_options = {"packages": [
        'requests', 'time', 'sys', 'sqlite3', 'datetime'], "excludes": ["tkinter"], 'include_files': [os.path.join(sys.base_prefix, 'DLLs', 'sqlite3.dll')]}
    
    setup(name='steamSpy',
      version='0.2',
      description='Gather_data',
      options={"build_exe": build_exe_options},
      executables=[Executable("steamSpy.py")])