简体   繁体   English

python中从sql中提取数据(每秒一次)

[英]Extract data from sql in python (one time per second)

how do i extract data from sql in python?如何从 python 中的 sql 中提取数据? I have this code but it is repeating all the values which is not what i want.我有这段代码,但它重复了所有不是我想要的值。 I wish to have new updated sql data to be in my python. I am almost struck for a few days..Any kind soul pls I am using sqlserver2012, python我希望在我的 python 中有新的更新 sql 数据。几天来我几乎被击中了..任何善良的灵魂请我使用 sqlserver2012, python

import time
sql_conn = connectSQLServer('ODBC Driver 13 for SQL Server', 'DESKTOP-K6A10IO\SQLEXPRESS', 'display')

mycursor = sql_conn.cursor()

a = 0
while True:
    try:
        time.sleep(1)
        a=a+1
        print(a)
        sql = "SELECT * FROM dbo.LiveStatsFromSQLServer"    
        mycursor.execute(sql)

        myresult = mycursor.fetchall()

        for x in myresult:
            print(x)


    except Exception as error:
        print("Nothing in database\n")
        print(error)
sql_conn.commit()
The result shown is 
1
(1625, 3)
(1626, 0)
(1627, 10)
2
(1625, 3)
(1626, 0)
(1627, 10)
3
(1625, 3)
(1626, 0)
(1627, 10)
(1622, 20)
while i wish to have a constantly updating to the end of the list like:
(1625, 3)
(1626, 0)
(1627, 10)
after 1 second
(1622, 20)
after 1 second
(1612, 10)

If I understand this right, you only want to see new records (those added since the previous query).如果我没理解错的话,您只想查看记录(自上次查询以来添加的记录)。 You need to add some logic in your application.您需要在应用程序中添加一些逻辑。

Your table probably already has an auto-incremented ID (or a timestamp of some sort could do).您的表可能已经有一个自动递增的 ID(或者某种时间戳也可以)。 After each query, you need to memorize the maximum value you got, and the subsequent query should include a WHERE clause (ie WHERE ID > nnnnn ) so that you only get records having an ID greater than that maximum value from your previous query.每次查询后,您需要记住您获得的最大值,并且后续查询应包含一个WHERE子句(即WHERE ID > nnnnn ),以便您只会获得 ID大于上一次查询的最大值的记录。

Alternatively you could use a datetime value but this could be less accurate (unless you don't mind some overlap).或者,您可以使用日期时间值,但这可能不太准确(除非您不介意某些重叠)。

This query simply returns everything.此查询仅返回所有内容。

PS: I don't know why you have a commit in your exception handling block. PS:我不知道为什么你的异常处理块中有一个提交。 You are not doing any transaction here, you are not writing any data to the DB either.你在这里没有做任何交易,你也没有向数据库写入任何数据。 Variable a is not used.不使用变量a

If editing the table schema is not an option it may be possible to get close to what you want with the following line...如果编辑表架构不是一个选项,则可以通过以下行接近您想要的内容......

  for row in cursor.execute("""SELECT num,id ,row_number() partition by (select 1) FROM dbo.LiveStatsFromSQLServer"""): 

Edit编辑

Seeing you can change the schema, add a column for dating edited in the table you are querying.看到您可以更改架构,在您正在查询的表中添加一个日期编辑列。

 for row in cursor.execute("""SELECT num,id FROM dbo.LiveStatsFromSQLServer where datetime> dateadd(s,-1,getdate())"""): 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM