如何使用 python SQL 服务器中 table 的 stream/print 几个最后附加的数据?
How to stream/print the several last appended data from a table in SQL Server using python?
我的 SQL 服务器中有一个 table 每分钟更新一次。
目前,我使用以下代码行从 table 获取数据:
conn = pymssql.connect(server, user, password, "tempdb")
def print_table():
cursor = conn.cursor(as_dict=True)
cursor.execute('SELECT * FROM EmotionDisturbances WHERE name=%s', 'John Doe')
for row in cursor:
#Show the data:
print("rate=%d, emotion=%s" % (row['rate'], row['emotion']))
conn.close()
在我的应用程序中,我 运行 每 10 秒执行一次该函数。
如何更新函数以便仅打印来自 table?
的最后附加数据
谢谢
假设您在 id
列中有一个自动递增索引,您会这样做:
SELECT * FROM EmotionDisturbances WHERE name = % ORDER BY id DESC LIMIT 1
编辑:如果您想要在特定时间后添加的所有数据,那么您需要迁移您的架构以具有 created
日期列(如果它还没有日期列),那么您可以做:
SELECT *
FROM EmotionDisturbances
WHERE name = % AND created >= DATEADD(second, -10, GETDATE())
这将获取过去 10 秒内创建的所有记录,因为您说此函数每 10 秒运行一次。
我的 SQL 服务器中有一个 table 每分钟更新一次。 目前,我使用以下代码行从 table 获取数据:
conn = pymssql.connect(server, user, password, "tempdb")
def print_table():
cursor = conn.cursor(as_dict=True)
cursor.execute('SELECT * FROM EmotionDisturbances WHERE name=%s', 'John Doe')
for row in cursor:
#Show the data:
print("rate=%d, emotion=%s" % (row['rate'], row['emotion']))
conn.close()
在我的应用程序中,我 运行 每 10 秒执行一次该函数。 如何更新函数以便仅打印来自 table?
的最后附加数据谢谢
假设您在 id
列中有一个自动递增索引,您会这样做:
SELECT * FROM EmotionDisturbances WHERE name = % ORDER BY id DESC LIMIT 1
编辑:如果您想要在特定时间后添加的所有数据,那么您需要迁移您的架构以具有 created
日期列(如果它还没有日期列),那么您可以做:
SELECT *
FROM EmotionDisturbances
WHERE name = % AND created >= DATEADD(second, -10, GETDATE())
这将获取过去 10 秒内创建的所有记录,因为您说此函数每 10 秒运行一次。