0

How to take PostgreSQL table's data constantly using Python? (For example, every 1 minute, it needs to take the next row's data) Could you please help me with that?

import psycopg2

#establishing the connection
conn = psycopg2.connect(
   database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432'
)

#Setting auto commit false
conn.autocommit = True

#Creating a cursor object using the cursor() method
cursor = conn.cursor()

#Retrieving data
cursor.execute('''SELECT * from EMPLOYEE''')

#Fetching 1st row from the table
result = cursor.fetchone();
print(result)

#Fetching 1st row from the table
result = cursor.fetchall();
print(result)

#Commit your changes in the database
conn.commit()

#Closing the connection
conn.close()
4
  • Did you considered using while loop with time ? Commented Nov 4, 2022 at 5:11
  • Yes, I found a way. It's now taking data constantly from database. I have another issue now. Could you please help me with it ? Commented Nov 4, 2022 at 5:54
  • In database table, Second and third columns have numbers. *Suppose 105 is selected from the second column and 115 is selected from the third column: *When the new row's number received in the .sql table drops below 105, the python should print a Notification message. "Warning! The number has dropped below 105.". On the other hand, whenever the number reveived from the third column is higher than 115, it needs to print "The number is higher than 115". Otherwise, python doesn't needs to give a message. I hope you got it. Commented Nov 4, 2022 at 5:54
  • Each time, whenever new rows added in database table, python needs to check them constantly. Commented Nov 4, 2022 at 5:54

1 Answer 1

1

You can use while loop and timer to execute your code as shown below.

import psycopg2
import time


#establishing the connection
conn = psycopg2.connect(
   database="mydb", user='postgres', password='password', host='127.0.0.1', port= '5432'
)

#Setting auto commit false
conn.autocommit = True

#Creating a cursor object using the cursor() method
cursor = conn.cursor()


def fetch_data(): 

    
    #Retrieving data
    cursor.execute('''SELECT * from EMPLOYEE''')
    
    #Fetching 1st row from the table
    result = cursor.fetchone();
    print(result)
    
    #Fetching 1st row from the table
    result = cursor.fetchall();
    print(result)
    
    #Commit your changes in the database
    conn.commit()
    

while True:
    fetch_data()
    print("Fetching data every one minute")
    time.sleep(1*60) # every sixty sec
    
    


Sign up to request clarification or add additional context in comments.

1 Comment

Bro, could you please add on your code this stuffs. Second and third columns have numbers. *Suppose 105 is selected from the second column and 115 is selected from the third column: *When the new row's number received in the .sql table drops below 105, the python should print a Notification message. "Warning! The number has dropped below 105.". On the other hand, whenever the number reveived from the third column is higher than 115, it needs to print "The number is higher than 115". Otherwise, python doesn't needs to give a message. I hope you got it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.