python - 无法使用 PyMySQL 插入日期

标签 python mysql python-2.7 pymysql

插入语句如下所示:

INSERT INTO events(ts, domain, url, lat_lon, sha1_hash)
VALUES (STR_TO_DATE(%s, '%Y-%m-%dT%H:%i:%sZ'), %s, %s, %s, sha1(url))

当我尝试使用 executemany 发送批处理时,出现此错误:

Traceback (most recent call last):
  File "/vagrant/workspace/gdelt/gdelt_all.py", line 41, in <module> read_files()
  File "/vagrant/workspace/gdelt/gdelt_all.py", line 33, in read_files cursor.executemany(INSERT_FOUR, four)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 195, in executemany self.rowcount = sum(self.execute(query, arg) for arg in args)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 195, in <genexpr> self.rowcount = sum(self.execute(query, arg) for arg in args)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 164, in execute query = self.mogrify(query, args)
  File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 143, in mogrify query = query % self._escape_args(args, conn)
ValueError: unsupported format character 'Y' (0x59) at index 83

我尝试转义 Y 以查看是否是正确的起点,但这没有帮助。有什么想法吗?

最佳答案

Escape %s 用于指定日期格式;这样它就不会被 Cursor.execute*(...)

处理
insert into events(ts, domain, url, lat_lon, sha1_hash) values 
(STR_TO_DATE(%s, '%%Y-%%m-%%dT%%H:%%i:%%sZ'), %s, %s, %s, sha1(url))

关于python - 无法使用 PyMySQL 插入日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44846253/

相关文章:

python-2.7 - 稀疏矩阵值错误 : row index exceeds matrix dimensions

python - 在 sympy 中隔离多元多项式的一个系数的最佳方法

python - 如何从 lxml 树中剥离 namespace ?

MySql 聚合计算

php - 为 foreach() 警告提供的参数无效

php - 2个条件适用的sql更新语句

python - 将货币对象更改为小数。使用小数实例

Python字典键(类对象)与多个比较器的比较

python - 查找哪些点在二维区域中

python - Python 2.7 中的奇怪异常.SystemExit