【问题标题】:how to use mysql function in python如何在python中使用mysql函数
【发布时间】:2021-03-16 08:10:53
【问题描述】:

我正在尝试使用 MySQL 函数 select curdate() 在表中插入日期,代码如下所示:

cur.execute("insert into attendance values(%s,%s,%s)",(input("enter absentee name "),select curdate(),"absent"))

显示语法错误

【问题讨论】:

  • 能否提供完整的错误信息?
  • ^ SyntaxError: 无效语法
  • 您的屏幕截图不安全。使窗口尽可能宽,直到整个语句在一行中。然后显示窗口的一部分,其中显示了插入符号和代码部分。
  • 我是堆栈溢出的新手,感谢您的帮助。下次一定要找对ss,谢谢

标签: python mysql mysql-python


【解决方案1】:

也许只是

cur.execute("insert into attendance values(%s,curdate(),%s)",(input("enter absentee name  "),"absent"))

?

【讨论】:

  • @GauravMeena 我无法预测错误消息 - 显示其完整文本。
  • runfile('E:/学校出勤 2.0.py', wdir='E:') 文件“E:\学校出勤 2.0.py”,第 7 行 cur.execute("插入出勤values(%s,curdate(),%s)",(input("输入缺席者姓名"),select curdate(),"absent")) ^ SyntaxError: invalid syntax
  • @GauravMeena 在插入符号^ 符号下有什么符号/位置?
  • 我附上了问题本身的错误截图,希望对您有所帮助
  • thnx 代码正在运行,我忘了删除选择 curdate(),谢谢
【解决方案2】:

语法错误来自select currdate() 不在字符串中。这不是有效的python。

cur.execute("insert into attendance values(%s,%s,%s)",(input("enter absentee name  "),select curdate(),"absent"))

至于实现你想要做的,试试

cur.execute("insert into attendance values(%s,curdate(),%s)",(input("enter absentee name  "),"absent"))

【讨论】:

  • 是的,已经尝试过了,它仍然显示语法错误。
  • 是Python语法错误还是SQL语法错误?以上本身就是有效的python。你能粘贴你的完整文件和/或整个错误吗?语法错误消息通常会告诉您它认为问题出在哪里。
  • 刚刚在问题本身中添加了错误截图希望有所帮助
  • 好吧,是的,你的回答是对的,我忘了删除 select curdate() 非常感谢
猜你喜欢
  • 2021-04-23
  • 2010-09-20
  • 1970-01-01
  • 2016-11-04
  • 2013-11-14
  • 2021-07-31
  • 2019-07-08
  • 2018-07-10
  • 2023-03-25
相关资源
最近更新 更多