What I’m trying to do:
I am following this link :
What I’ve tried and what’s not working:
Code Sample:
(env) (base) girish@girish-System-Product-Name:~/myapps1$ cat inventory.py
import psycopg2
conn = psycopg2.connect(
host="18.133.244.120",
user="xxxxxx", <--- Here I have given correct user name as shown on the page
password="yyyyyyyyy") <--- Here I have given correct user name as shown on the page
conn.set_session(autocommit=True)
cur = conn.cursor()
def get_items():
cur.execute('SELECT (id, item_name, quantity) FROM inventory;')
items = cur.fetchall()
return [
{'id': item[0], 'name': item[1], 'quantity': item[2]}
for item in items
]
print(get_items())
(env) (base) girish@girish-System-Product-Name:~/myapps1$ python3 inventory.py
Traceback (most recent call last):
File "inventory.py", line 19, in <module>
print(get_items())
File "inventory.py", line 14, in get_items
return [
File "inventory.py", line 15, in <listcomp>
{'id': item[0], 'name': item[1], 'quantity': item[2]}
IndexError: tuple index out of range
(env) (base) girish@girish-System-Product-Name:~/myapps1$ psql -h 18.133.244.120 -U xxxxxxxx <--- Here I have given correct user name as shown on the page
Password for user xxxxxxx: <--- Here I have given correct user name as shown on the page
psql (13.4 (Ubuntu 13.4-0ubuntu0.21.04.1), server 12.1 (Debian 12.1-1.pgdg100+1))
Type "help" for help.
obvious_neat_self=> select * from inventory;
id | item_name | quantity
----+----------------+----------
1 | Vase | 10
2 | Bookcase | 5
3 | Bathtowel | 20
4 | Frying Pan | 20
5 | Large Saucepan | 25
6 | Small Saucepan | 25
7 | Dinner Plate | 15
8 | Dining Chair | 10
(8 rows)
obvious_neat_self=> \q
(env) (base) girish@girish-System-Product-Name:~/myapps1$
But I am getting python error as shown above. Kindly help me how do I correct it. Is it a tutorial bug?