Skip to content

Instantly share code, notes, and snippets.

@pablodz
Last active July 1, 2020 21:32
Show Gist options
  • Select an option

  • Save pablodz/536d39ae83b071cdbba4cf0e2042585c to your computer and use it in GitHub Desktop.

Select an option

Save pablodz/536d39ae83b071cdbba4cf0e2042585c to your computer and use it in GitHub Desktop.
import psycopg2
import random
import time
host="localhost"
database="your_database_name"
user="your_user"
pwd="your_password"
p="5432" # Default port
def make_connection():
conn = psycopg2.connect(database=database,
user=user,
password=pwd,
host=host,
port=p)
print(conn)
return conn
def execute_query(conn,t,h):
c = conn.cursor()
c.execute("""INSERT INTO conditions(time, location, temperature, humidity) VALUES (NOW(), 'office', {}, {});""".format(t,h))
conn.commit()
def generate_random_data(conn):
minutes=10
t=20
h=45
for i in range(2*60*minutes):
t+=random.uniform(-1, 1)
h+=random.uniform(-2, 2)
execute_query(conn,t,h)
time.sleep(0.5) # Comment this line
print(i,end=" , ")
def close_connection(conn):
conn.close()
if __name__ == "__main__":
conn=make_connection()
generate_random_data(conn)
close_connection(conn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment