I have created a database table called fruits with 3 columns, id(int primary key), fruits(text) and weight(float).
| id | fruit | weight |
|---|---|---|
| 1 | Apple | 80.5 |
| 2 | Pear | 150.8 |
| 3 | Kiwi | 69 |
How do I create a dictionary and add all the fruits and weight as key-value pairs to the dictionary?
import sqlite3
conn = sqlite3.connect("database.db")
sql = """SELECT fruit,weight FROM fruits"""
cursor = conn.execute(sql)
data = cursor.fetchall()
fruitweight= {}
i = 0
while(i < len(data)):
fruitweight['<fruitname>'] = <fruitweight>
i = i+1