I know table name, have SQLLite .db file. I want to not knowing its schema iterate thru an array of dictionaries (an item per each record containing pairs: column name : data
). How to do such thing with sqlalchemy and python?
Asked
Active
Viewed 106 times
-1

DuckQueen
- 772
- 10
- 62
- 134
-
1`SELECT * FROM tablename;`? For SQLAlchemy you will need to have a model, which you might retrieve with [introspection](https://stackoverflow.com/questions/11330013/sqlalchemy-introspection). – Klaus D. Jun 22 '19 at 16:55
-
What does this have to do with JSON? – roganjosh Jun 22 '19 at 17:22
-
Please have a look at https://docs.sqlalchemy.org/en/13/core/tutorial.html. You don't need a model. – Ilja Everilä Jun 22 '19 at 18:06
1 Answers
1
Assuming you want to do this in Python:
import sqlite3
conn=sqlite3.connect("YourDatabase.db")
c=conn.cursor()
table=list(c.execute("""SELECT * FROM YourTable"""))
columns=list(c.execute("""PRAGMA table_info('YourTable')"""))
recordicts=[]
for record in table:
dict1={}
for i in range(len(record)):
dict1.update({columns[i][1] : record[i]})
recordicts.append(dict1)
print(recordicts)
Hope this helps! -Theo

Theo
- 613
- 4
- 22