从python .exe文件创建和更新SQLite数据库(使用cx_Freeze)

时间:2017-03-25 23:49:55

标签: python sqlite cx-freeze

问候!这是我在这个论坛使用了几年之后的第一个问题。我会尽力遵循指南,请对我的问题提出改进建议。

我的程序从JSON字符串中的steamspy.com API收集信息。这些都写入SQLite数据库。

该程序直接从 .py 文件运行没有问题,但是,我希望能够将我的程序作为单个可执行文件运行和分发。

我曾使用cx_Freeze创建 .exe 文件,但该程序不会生成SQLite数据库。我已经在这个论坛和网络上广泛搜索了一个解决方案,没有任何运气。

下面我粘贴了我的主程序代码 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.")

接下来是 setup.py 文件的代码:

from cx_Freeze import setup, Executable

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

这很可能是微不足道的信息,但如果有人有疑问,可以在命令提示符中使用以下行之一创建可执行文件:

  

python setup.py build

     

python setup.py bdist_msi

1 个答案:

答案 0 :(得分:1)

setup.py应包含python程序中使用的所有模块。 并且sqlite3还需要一个特定的.dll文件。

试试这个setup.py,应该可以。

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")])