如何通过Python更新MySQL中的时间戳字段?
我有以下模式的数据库表 模式如何通过Python更新MySQL中的时间戳字段?,python,mysql,mysql-python,Python,Mysql,Mysql Python,我有以下模式的数据库表 模式 hash VARCHAR(32)不为NULL,区域单元VARCHAR(64)不为NULL,url VARCHAR(2048)不为NULL,在时间戳不为NULL时创建默认值“0000-00-00 00 00:00:00”,在时间戳不为NULL时更新默认值NOW(),主键(hash) 我通过Python使用以下语句更新: 查询 插入url(散列、区域单位、url、创建地点、更新地点) 值(%s,%s,%s,%s,%s) 重复密钥更新时哈希=哈希; [[“…”、“…”、“
hash VARCHAR(32)不为NULL,区域单元VARCHAR(64)不为NULL,url VARCHAR(2048)不为NULL,在时间戳不为NULL时创建默认值“0000-00-00 00 00:00:00”,在时间戳不为NULL时更新默认值NOW(),主键(hash)
我通过Python使用以下语句更新:
查询
插入url(散列、区域单位、url、创建地点、更新地点)
值(%s,%s,%s,%s,%s)
重复密钥更新时哈希=哈希;
[[“…”、“…”、“…”,无,datetime.datetime(2015,2,20,21,18,55910026)],
[“…”,“…”,“…”,无,datetime.datetime(2015,2,20,21,18,55910046)]]
我的问题是,根据上述语句,字段'created\u at'
不会更新,即它始终与'created\u at'
字段具有相同的值
编辑:
INSERT INTO urls (hash, regional_unit, url, created_at, updated_at) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE hash=hash; [['...', '...', '...', datetime.datetime(2015, 2, 20, 22, 12, 57, 268424), datetime.datetime(2015, 2, 20, 22, 12, 57, 268437)], ['...', '...', '...', datetime.datetime(2015, 2, 20, 22, 12, 57, 268444), datetime.datetime(2015, 2, 20, 22, 12, 57, 268446)]]
self.db_connect()
self.get_curs().executemany("""INSERT INTO urls (hash, regional_unit, url, created_at, updated_at) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE hash=hash;""", [['...', '...', '...', datetime.datetime(2015, 2, 20, 22, 12, 57, 268424), datetime.datetime(2015, 2, 20, 22, 12, 57, 268437)], ['...', '...', '...', datetime.datetime(2015, 2, 20, 22, 12, 57, 268444), datetime.datetime(2015, 2, 20, 22, 12, 57, 268446)]])
self.get_conn().commit()
self.db_disconnect()
EDIT2:
INSERT INTO urls (hash, regional_unit, url, created_at, updated_at) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE hash=hash; [['...', '...', '...', datetime.datetime(2015, 2, 20, 22, 12, 57, 268424), datetime.datetime(2015, 2, 20, 22, 12, 57, 268437)], ['...', '...', '...', datetime.datetime(2015, 2, 20, 22, 12, 57, 268444), datetime.datetime(2015, 2, 20, 22, 12, 57, 268446)]]
self.db_connect()
self.get_curs().executemany("""INSERT INTO urls (hash, regional_unit, url, created_at, updated_at) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE hash=hash;""", [['...', '...', '...', datetime.datetime(2015, 2, 20, 22, 12, 57, 268424), datetime.datetime(2015, 2, 20, 22, 12, 57, 268437)], ['...', '...', '...', datetime.datetime(2015, 2, 20, 22, 12, 57, 268444), datetime.datetime(2015, 2, 20, 22, 12, 57, 268446)]])
self.get_conn().commit()
self.db_disconnect()
试着这样做:
a = datetime.datetime(2015, 2, 20, 22, 12, 57, 268437).strftime('%Y-%m-%d %H:%M:%S')
self.get_curs().executemany("""INSERT INTO urls (hash, regional_unit, url, created_at, updated_at) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE hash=hash;""", [['...', '...', '...', a, a], ['...', '...', '...', a,a]])
created_at
是查询中的第四个字段,出于某种原因,您已将其设置为None
;-)不。不幸的是,这似乎不是问题所在。我编辑了我的问题,但仍然遇到同样的问题……但这是python代码吗?我没有看到python结构。并且看不到查询字符串。你把一切都混在一起了。显示您使用的真实python代码好的,我也引用了我的python代码。请看一下你不介意:)@KimAlexander请看一下EDIT2
:)不幸的是,这也不起作用。。。我还应该尝试什么?所以在开始插入之前,您的表是空的?它会插入除在处创建的之外的所有内容?我的表在第一次运行查询时确实是空的。但是第二次,当表已经有一条记录时,由于某种原因,更新的\u处的字段没有得到更新。。。有什么想法吗?嗯,没什么,你到底是什么意思。。。从技术上讲,我不使用UPDATE,而是使用INSERT的一个特例。是的,但是您在DUPLICATE上编写了<代码>更新哈希值
。。。因此,除了散列
之外,您甚至没有尝试更新任何字段,甚至您没有尝试更新的散列,您只是要求保留旧值