用pycharm运行没问题,但是打包之后会报错。表已经创建了没问题,exe根目录也会生成.db文件
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
先用pycharm运行后,再把生成的.db文件拷贝到exe根目录就好了,但是为什么直接运行exe在根目录生成的.db文件不能用呢,还有怎么才能把依赖文件直接打包呢
def create_database():
base_dir = os.path.dirname(os.path.abspath(__file__))
db_path = base_dir + '\data_ticketcounter.db'
conn = sqlite3.connect(db_path)
print("Database connected successfully")
c = conn.cursor()
try:
c.execute('''CREATE TABLE helpers (
'appid' INT(11) NOT NULL,
'name' VARCHAR(255) NOT NULL,
'count' INT(11) NOT NULL,
'city' VARCHAR(255) NULL DEFAULT NULL,
'touch_delay' INT(11) NULL DEFAULT NULL,
'avg_csi' INT(11) NULL DEFAULT NULL,
'is_outsourcing' INT(11) NULL DEFAULT NULL,
'resolved_count' INT(11) NULL DEFAULT NULL,
'touched_count' INT(11) NULL DEFAULT NULL,
'tcs_project_name' VARCHAR(500) NULL DEFAULT NULL,
'department' VARCHAR(255) NULL DEFAULT NULL,
'resolve_delay' INT(11) NULL DEFAULT NULL,
'csi_count' INT(11) NULL DEFAULT NULL,
'email' VARCHAR(255) NULL DEFAULT NULL,
'timenow' DATE NULL DEFAULT NULL,
PRIMARY KEY ('appid', 'name'));''')
conn.commit()
print("Create table1 successfully")
except sqlite3.OperationalError:
print("table already exists")
def save_app_infos(data): # 构建数据库,存储数据,目标是可自动更新
conn = sqlite3.connect('data_ticketcounter.db')
print("Database connected successfully")
c = conn.cursor()
table = 'helpers'
data['timenow'] = datetime.date.today()
keys = ','.join(data.keys())
values = ','.join(['?'] * len(data))
sql_insert = 'INSERT INTO {table}({keys}) VALUES ({values}) ON CONFLICT(appid, name) DO UPDATE SET '.format(table=table, keys=keys,
values=values)
update = ','.join(['{key}=?'.format(key=key) for key in data])
sql_insert += update
try:
if c.execute(sql_insert, tuple(data.values()) * 2):
print("执行成功")
conn.commit()
except Exception as e:
print(e)
conn.rollback()
conn.close()