execute e executemany com dicionários e lista de dicionários

This commit is contained in:
Luiz Otávio
2023-04-01 14:58:22 -03:00
parent 074a2f3cdf
commit e616b27e2f

View File

@@ -34,15 +34,22 @@ sql = (
f'INSERT INTO {TABLE_NAME} ' f'INSERT INTO {TABLE_NAME} '
'(name, weight) ' '(name, weight) '
'VALUES ' 'VALUES '
'(?, ?)' '(:nome, :peso)'
) )
# cursor.execute(sql, ['Joana', 4]) # cursor.execute(sql, ['Joana', 4])
cursor.executemany( # cursor.executemany(
sql, # sql,
( # (
('Joana', 4), ('Luiz', 5) # ('Joana', 4), ('Luiz', 5)
) # )
) # )
cursor.execute(sql, {'nome': 'Sem nome', 'peso': 3})
cursor.executemany(sql, (
{'nome': 'Joãozinho', 'peso': 3},
{'nome': 'Maria', 'peso': 2},
{'nome': 'Helena', 'peso': 4},
{'nome': 'Joana', 'peso': 5},
))
connection.commit() connection.commit()
print(sql) print(sql)