Rows as dictionaries

EasySQLite Programs · 8 of 20

Write a Python program to read rows by column name.

Advertisement
Output
Asha Pune {'name': 'Asha', 'city': 'Pune'}

Approach

  1. conn.row_factory = sqlite3.Row.

Solution

Python
import sqlite3 conn = sqlite3.connect(":memory:")conn.row_factory = sqlite3.Rowconn.execute("CREATE TABLE users (name TEXT, city TEXT)")conn.execute("INSERT INTO users VALUES ('Asha', 'Pune')")row = conn.execute("SELECT * FROM users").fetchone()print(row["name"], row["city"], dict(row))
Output
Asha Pune {'name': 'Asha', 'city': 'Pune'}

Learn the concept

New to this topic? Read the lesson, then come back.

More sqlite programs

← All 20 SQLite Programs

Leave a Comment