python3 ,input的量怎么在sql语法里面使用

老壶 2019-11-09 07:36:40
python3 input变量能不能在sql语法里面调用


begin=input('请输入开始日期')
end=input('请输入开始日期')

result = cursor.execute(
'select pro_class,pro_id,pro_name,pro_spec,pro_unit,pro_city, \
pro_normal_price ,pro_last_in_price ,pro_special_code , \
sum(psl_qty),sum(psl_qty*psl_unit_cost),sum(psl_amt) \
from pos.product,pos.p_sale \
where psl_date>= begin \
and psl_date<= end \
and psl_pro_id = pro_id\
group by pro_class,pro_id,pro_name,pro_spec,pro_unit,\
pro_city,pro_normal_price,pro_last_in_price,pro_special_code '
...全文
116 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
编程写手 2019-11-25
  • 打赏
  • 举报
回复
可以直接使用占位符方式即可
老壶 2019-11-10
  • 打赏
  • 举报
回复
引用 1 楼 seakingx 的回复:
大概如此

s1 = '1'
s2 = '100'
sql_str = 'select * from tbl1 where fld > {} and fld < {} '.format(s1,s2)
print(sql_str)
cursor.execute(sql_str)

cx_Oracle.DatabaseError: ORA-00932: 数据类型不一致: 应为 DATE, 但却获得 NUMBER 这个提示
陈年椰子 2019-11-10
  • 打赏
  • 举报
回复
如果是 ORACLE , 输入为 字符串, 字段是date型, 你的SQL 语句里要用 to_date('20191101','yyyymmdd') 去转换。 不能直接比较。
陈年椰子 2019-11-09
  • 打赏
  • 举报
回复
大概如此

s1 = '1'
s2 = '100'
sql_str = 'select * from tbl1 where fld > {} and fld < {} '.format(s1,s2)
print(sql_str)
cursor.execute(sql_str)

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧