Reputation: 147
I have a constantly updated database (5s Interval value) and my intention is to read the new data that has been updated in the database, below is my first draft code. However, I managed to read it until the last line at the time the code is executed and then it stop. I need the code to constantly run so that it fetch the new data entry (our database is updated every 5 second).
import csv
import pyodbc
server = '*****'
database = '******'
username = '******'
password = '*****'
cnxn = pyodbc.connect('DRIVER={SQL SERVER};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("select * from marketc")
row = cursor.fetchone()
while row is not None:
row = cursor.fetchone()
print (row)
*it maybe hard to understand, but the main point is i just want to read the last row in our database in which the database is constantly updated for every 5 second.
Upvotes: 0
Views: 161
Reputation: 147
Managed to code it. I just import the time package then looping it and check the database again for every 6 second since I know a new values will be updated in database for every 5 second.
import csv
import pyodbc
import time
server = '*****'
database = '******'
username = '******'
password = '*****'
cnxn = pyodbc.connect('DRIVER={SQL SERVER};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("select * from marketc")
while True:
row = cursor.fetchone()
while row is not None:
row = cursor.fetchone()
print (row)
time.sleep(6)
Upvotes: 1