Python 重点补充:
PostgreSQL提供了许多 返回相关值的函数 到当前日期和时间。这些 SQL标准函数都返回 值,该值基于 当前交易:Python 重点补充:,python,postgresql,cherrypy,Python,Postgresql,Cherrypy,PostgreSQL提供了许多 返回相关值的函数 到当前日期和时间。这些 SQL标准函数都返回 值,该值基于 当前交易: 当前日期 当前时间 当前时间戳 当前时间(精度) 当前时间戳(精度) LOCALTIMELOCALTIMESTAMP LOCALTIME(精度) LOCALTIMESTAMP(精度) 当前时间和当前时间戳 利用时区传递价值观; LOCALTIME和LOCALTIMESTAMP 提供无时区的价值 当前时间,当前时间戳, LOCALTIME和LOCALTIMESTAMP可以
当前日期
当前时间
当前时间戳
当前时间(精度)
当前时间戳(精度)
LOCALTIME
LOCALTIMESTAMP
LOCALTIME(精度)
LOCALTIMESTAMP(精度)
当前时间
和当前时间戳
利用时区传递价值观;
LOCALTIME
和LOCALTIMESTAMP
提供无时区的价值
当前时间
,当前时间戳
,
LOCALTIME
和LOCALTIMESTAMP
可以
可以选择给定精度
参数,该参数将导致结果
四舍五入到那个分数
秒字段中的数字。没有
精度参数,结果为
以完全可用的精度
由于这些函数返回
当前事务的开始时间,
它们的值在测试期间不会更改
事务处理。这被认为是一个错误
功能:目的是允许
单笔交易有一个
“当前”的一致概念
时间,以便多次修改
在同一交易中,承担
同一时间戳
注意:其他数据库系统可能会更频繁地提升这些值
PostgreSQL还提供函数
返回的开始时间
当前声明以及
此时的实际当前时间
函数被调用。完整的清单
非SQL标准时间函数的类型为:
now()
transaction\u timestamp()
语句\u timestamp()
clock\u timestamp()
timeofday()
now()
是一种传统的PostgreSQL
相当于当前时间戳。
transaction\u timestamp()
也是如此
相当于当前时间戳,但
命名是为了清楚地反映它是什么
返回<代码>语句\u timestamp()
返回当前事件的开始时间
声明(更具体地说,时间
收到最新命令的时间
来自客户端的消息)。
语句\u timestamp()
和
transaction\u timestamp()
返回
在的第一个命令中使用相同的值
事务,但在
后续命令。
clock\u timestamp()
返回实际的
当前时间及其值
即使在单个SQL中也会发生更改
指挥部timeofday()
是一个历史记录
PostgreSQL函数。喜欢
clock\u timestamp()
,它返回
实际当前时间,但作为
格式化的文本字符串,而不是
带有时区值的时间戳
我刚刚做了这些更改并重新启动了cherrypy服务器,但仍然存在相同的问题。更新:我们从postgres 8.0迁移到了8.3,这个答案现在也可以了。感谢您的解释。我还没有测试你的建议,但是我已经接受了你的答案,因为我已经尽力解释了为什么时间戳是不正确的。但是,现在我想知道是否有一种方法可以在不启动事务的情况下创建游标。您可以将Psycopg2设置为事务隔离级别
隔离级别\u AUTOCOMMIT
,它在发出命令时不会启动事务。不过,我不知道这种变化会有多大范围;这样做可能会中断使用事务的其他查询。
CREATE TABLE test (given_time timestamp,
default_time timestamp DEFAULT NOW());
import sys
import datetime
import psycopg2
import cherrypy
def connect(thread_index):
# Create a connection and store it in the current thread
cherrypy.thread_data.db = psycopg2.connect('dbname=timestamps')
# Tell CherryPy to call "connect" for each thread, when it starts up
cherrypy.engine.subscribe('start_thread', connect)
class Root:
@cherrypy.expose
def index(self):
html = []
html.append("<html><body>")
html.append("<table border=1><thead>")
html.append("<tr><td>Given Time</td><td>Default Time</td></tr>")
html.append("</thead><tbody>")
for given, default in self.get_timestamps():
html.append("<tr><td>%s<td>%s" % (given, default))
html.append("</tbody>")
html.append("</table>")
html.append("<form action='add_timestamp' method='post'>")
html.append("<input type='submit' value='Add Timestamp'/>")
html.append("</form>")
html.append("</body></html>")
return "\n".join(html)
@cherrypy.expose
def add_timestamp(self):
c = cherrypy.thread_data.db.cursor()
now = datetime.datetime.now()
c.execute("insert into test (given_time) values ('%s')" % now)
c.connection.commit()
c.close()
raise cherrypy.HTTPRedirect('/')
def get_timestamps(self):
c = cherrypy.thread_data.db.cursor()
c.execute("select * from test order by given_time desc limit 10")
records = c.fetchall()
c.close()
return records
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '0.0.0.0',
'server.socket_port': 8081,
'server.thread_pool': 5,
'tools.log_headers.on': False,
})
cherrypy.quickstart(Root())
Given Time Default Time
2009-03-18 09:31:30.725017 2009-03-18 09:31:25.218871
2009-03-18 09:31:25.198022 2009-03-18 09:31:17.642010
2009-03-18 09:31:17.622439 2009-03-18 09:31:08.266720
2009-03-18 09:31:08.246084 2009-03-18 09:31:01.970120
2009-03-18 09:31:01.950780 2009-03-18 09:30:53.571090
2009-03-18 09:30:53.550952 2009-03-18 09:30:47.260795
2009-03-18 09:30:47.239150 2009-03-18 09:30:41.177318
2009-03-18 09:30:41.151950 2009-03-18 09:30:36.005037
2009-03-18 09:30:35.983541 2009-03-18 09:30:31.666679
2009-03-18 09:30:31.649717 2009-03-18 09:30:28.319693
Given Time Default Time
2009-03-18 09:38:15.906788 2009-03-18 09:33:58.839075
2009-03-18 09:37:19.520227 2009-03-18 09:37:19.520293
2009-03-18 09:36:04.744987 2009-03-18 09:36:04.745039
2009-03-18 09:35:05.958962 2009-03-18 09:35:05.959053
2009-03-18 09:34:10.961227 2009-03-18 09:34:10.961298
2009-03-18 09:33:58.822138 2009-03-18 09:33:55.423485
def add_timestamp(self):
c = cherrypy.thread_data.db.cursor()
now = datetime.datetime.now()
c.execute("insert into test (given_time) values ('%s')" % now)
c.connection.commit()
c.close()
raise cherrypy.HTTPRedirect('/')
def get_timestamps(self):
c = cherrypy.thread_data.db.cursor()
c.execute("select * from test order by given_time desc limit 10")
records = c.fetchall()
c.close()
return records
def get_timestamps(self):
c = cherrypy.thread_data.db.cursor()
c.execute("select * from test order by given_time desc limit 10")
records = c.fetchall()
c.connection.commit() # Adding this line fixes the timestamp issue
c.close()
return records