将格式化为日期时间的字符串插入我的数据库导致我出现此错误。有什么办法吗?

时间:2019-04-28 13:06:41

标签: python python-2.7 phpmyadmin pymysql

该脚本正在正确读取字符串,但似乎无法继续运行并插入到数据库中。

这是我的家庭项目。我正在使用RasPi 3 ModelB +。

a = input('Scan Card: ')
now = datetime.strftime('%H:%M:%S')

#This is the insert line
tapped = "INSERT INTO log (rfid, taptime) VALUES (%s, %s)" %(a, now)
t = conn.cursor()
t.execute(tapped)
conn.commit()
Traceback (most recent call last):
  File "door.py", line 54, in <module>
    t.execute(tapped)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 517, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 732, in _read_query_result
    result.read()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1075, in read
    first_packet = self.connection._read_packet()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 684, in _read_packet
    packet.check_error()
  File "/usr/local/lib/python2.7/dist-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, u"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':40:44)' at line 1")

我只想窃听卡片的日志数据库。

2 个答案:

答案 0 :(得分:0)

您不应使用字符串替换,而应将值作为参数传递给execute:

tapped = "INSERT INTO log (rfid, taptime) VALUES (%s, %s)")
t = conn.cursor()
t.execute(tapped, (a, now))

答案 1 :(得分:-1)

我不确定,但是错误在这里

现在= datetime.strftime('%H:%M:%S')

问题是您正在使用H,M和S。 尝试:date(“ Y-m-d H:i:s”)使用数字值

您必须以24小时制进行操作。