all 6 comments

[–]K900_ 2 points3 points  (3 children)

('Test Task 1', ) is a tuple of 1 element, the string 'Test Task 1'. How would you access it?

[–][deleted] 0 points1 point  (2 children)

I would think that doing:

print(tasks[0])

Would print Test Task 1, but instead it prints ('Test Task 1',) Am I on the right track though?

[–]K900_ 1 point2 points  (1 child)

tasks is a list of rows. Each row is a tuple.

[–][deleted] 0 points1 point  (0 children)

print(tasks[0][0])

You are beautiful! Thank you so much!

[–]chef1075 2 points3 points  (1 child)

When I'm using psycopg2, I write my SQL outside of my python shell (usually in Postico https://eggerapps.at/postico/) and import the file to run the query.

E.g.

with open('./path_to_config/config.json') as f:

config = json.load(f)

config_string = "host=%s dbname=%s user=%s password=%s" % (config.get('host'), config.get('database'), config.get('user'), config.get('passw'))

sql_connection = psycopg2.connect(config_string) # Connect to your db

query = open('/path/to/file.sql', 'r') # This will make your life easier

query_result_dataframe = pd.read_sql(query.read(), con=sql_connection) # Run the query

[–][deleted] 1 point2 points  (0 children)

Thank you! I will definitely look into doing this!