【发布时间】:2014-07-17 20:26:47
【问题描述】:
我有循环的代码,向每一行添加一行信息。但是,我发现每一行都没有新的时间戳,而是与第一行相同,这让我相信 current_timestamp 的值不会每次都更新。那么,有什么办法解决这个问题呢?这是我的代码:
if __name__ == "__main__":
main()
deleteAll() # Clears current table
ID = 0
while ID < 100:
insert(ID, 'current_date', 'current_timestamp')
ID += 1
conn.commit()
我的插入函数:
def insert(ID, date, timestamp): # Assumes table name is test1
cur.execute(
"""INSERT INTO test1 (ID, date,timestamp) VALUES (%s, %s, %s);""", (ID, AsIs(date), AsIs(timestamp)))
这段代码在 python 中,顺便说一句,它使用 postgresql 处理数据库。
【问题讨论】:
-
时间戳字段的类型是什么。 mysql
timestamp-type 字段完全符合您的要求。但是,如果您的timestamp字段实际上是datetime类型,那么您每次都必须使用触发器或使用新值手动更新它。 -
什么是
AsIs('current_timestamp')? -
@MarcB 是的,它是一个日期时间类型。你知道如何每次手动更新一个新值吗?我认为它已经这样做了,但我不确定它是一个日期时间类型如何改变它。谢谢!
-
把它改成
timestamp类型,mysql会在你update记录的任何时候自动为你更新。
标签: python postgresql timestamp psycopg2