首页 > 解决方案 > Python sqlite 查询在终端中工作,但不能作为 exe

问题描述

我正在编写一个从 sqlite 数据库中提取用户信息的 Python 脚本。当我在终端中手动运行脚本时,我的查询有效。但是当我将脚本作为可执行文件运行时,sqlite 返回一个操作错误。

代码:

import datetime
import sqlite3

def get_user_info():

    user_id = input("type the user's id number:")
    # connect with the  database 
    connection = sqlite3.connect('test.db')

    # sql query
    query = '''
    SELECT * 
    FROM c_list 
    WHERE user_id = ?
    '''
    crsr = connection.execute(query, (user_id))
    
    for row in crsr:
        print("Username : ", row[0])
        print("User ID :", row[1])
        print("Book(s) checked out :", row[2])
        print("Date checked out :", row[3])
        print("\n")

    connection.commit()
    connection.close()

    answer = input("would you like to return a book?")
    if answer == 'yes':
        return_book()
    else:
        exit()

get_user_info()

错误:

Traceback (most recent call last):
  File "test.py", line 42, in <module>
  File "test.py", line 24, in get_user_info
sqlite3.OperationalError: no such table: c_list
[7892] Failed to execute script test

标签: pythonsqliteterminalexe

解决方案


推荐阅读