简体   繁体   中英

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

how do i extract data from sql in python? 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

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). 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.

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. You are not doing any transaction here, you are not writing any data to the DB either. Variable a is not used.

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())"""): 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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