目前我尝试要求用户在Python中输入,输入一些信息(此处:费用ID、费用、类别ID、日期)。但不知从何下手。此步骤不需要输入验证。
我设法访问我的数据库并手动插入一些内容。我尝试了 python
input
函数的几种方法,但无法将其用作 SQL 字符串中的占位符。
import sqlite3
with sqlite3.connect('Expenses.sqlite') as conn:
# INSERT MANUALLY
script = "INSERT INTO Expense (ExpenseId, Amount, CategoryId, Date) VALUES ('103', '43625.5', '5', '2019-01-20');"
conn.execute(script) # execute the script
conn.commit() # commit changes to the file
# INSERT USER INPUT ???
pass
这是我的想法:
with sqlite3.connect('Expenses.sqlite') as conn:
amount = input("What is the amount?")
script = "SELECT * FROM Category;"
conn.execute(script)
print(script)
category = input("What is the category?")
exp_ID = "SELECT LAST ExpenseId FROM Expense);"
date = datetime.date.today()
script = "INSERT INTO Expense (ExpenseId, Amount, CategoryId, Date) VALUES (exp_ID, amount, category, date);"
conn.execute(script)
conn.commit()
pass
最后我想实现先询问用户费用金额,然后询问费用类别。应自动添加 ExpenseID 和日期。日期格式为年-月-日。非常感谢您的建议。
使用 input 函数检索用户输入
user_input = input("Expense Amount: ")
然后将 placeholders 与 sqlite3 一起使用
sql = "INSERT INTO TABLE (COLUMN) VALUES (?)"
conn.execute(sql, (user_input,))
**回应您的编辑
您需要添加占位符而不是变量名称。
类似这样的:
script = "INSERT INTO Expense (ExpenseId, Amount, CategoryId, Date) VALUES (?, ?, ?, ?);"
conn.execute(script, (exp_ID,amount,category,date))
选择 Sales_Subset 、 * 、 FGI 。 * , 顾客 / * 来自客户 内部联接 (FGI_产品.INNER JOIN Sales_子集 上 FGI_产品 。产品代码 = 销售子集。产品代码) ON 客户。客户 ID = 销售子集 。 Crustomer_ID;