I have simple example from a book about using SQLAlchemy on 1st level, which is close to DB-API. Since this book came out it's syntax changed and I have to modify code to have it worked. I have a problem inserting row in a table, using positional placeholders in a pre-writen string. Here is the code:
import sqlalchemy as sa
from sqlalchemy import text
engine = sa.create_engine('sqlite://')
with engine.connect() as conn:
print (conn)
conn.execute(text('''CREATE TABLE zoo (
critter VARCHAR(20) PRIMARY KEY,
count INT,
damage FLOAT)'''))
ins = text('INSERT INTO zoo (critter, count, damage) VALUES (?,?,?)')
conn.execute (ins, [('bear', 2, 1000.0)])
conn.execute (ins, [('weasel', 1, 2000.0)])
rows = conn.execute('SELECT * FROM zoo')
for row in rows:
print (row)
for some reason, it raises this:
'<' not supported between instances of 'int' and 'str'
...although I do not matching anything and values order seems to be right (for the corresponding columns). Please, help to understand what is wrong here?
dictionary
orlist of dictonaries
but notlist of tuple(s)
. As I remeber you can usetuple
when you use directly modulesqlite3
instead ofsqlalchemy