如何使用用户设置的日期参数从 SQL table 中 select 项目
how can I select items from an SQL table with user set date parameters
所以我有一个 SQL table,其中包含一个日期列。但是我想在我的程序中添加一个功能,用户可以在其中输入 2 个不同的日期(开始日期和结束日期),并且将打印 table 中介于这些日期之间的所有项目。这可能是我的一些代码的样子但是我不确定它是否有用
choice1 = input('would you like to view outflows from a specific time period? Or type "EXIT" to quit back to the main menu: ')
if choice1 == 'yes':
datechoice1 = input('date from (formtat YYYY-MM-DD): ')
datechoice2 = input('date to (formtat YYYY-MM-DD): ')
在这部分之后我不太确定从这里去哪里才能实现这个日期存储在 'outflow' table 中的变量被称为 'date'顺便说一句
任何 help/guidance 将不胜感激,谢谢 :)
您需要连接到数据库并执行类似
的查询
cursor.execute("""
Select * from database.table
Where datecolumn between %s and %s
""",
(datechoice1, datechoice2)
)
所以我有一个 SQL table,其中包含一个日期列。但是我想在我的程序中添加一个功能,用户可以在其中输入 2 个不同的日期(开始日期和结束日期),并且将打印 table 中介于这些日期之间的所有项目。这可能是我的一些代码的样子但是我不确定它是否有用
choice1 = input('would you like to view outflows from a specific time period? Or type "EXIT" to quit back to the main menu: ')
if choice1 == 'yes':
datechoice1 = input('date from (formtat YYYY-MM-DD): ')
datechoice2 = input('date to (formtat YYYY-MM-DD): ')
在这部分之后我不太确定从这里去哪里才能实现这个日期存储在 'outflow' table 中的变量被称为 'date'顺便说一句
任何 help/guidance 将不胜感激,谢谢 :)
您需要连接到数据库并执行类似
的查询cursor.execute("""
Select * from database.table
Where datecolumn between %s and %s
""",
(datechoice1, datechoice2)
)