Inserindo vários valores com uma consulta só usando iteráveis ou dicionários
This commit is contained in:
@@ -60,7 +60,42 @@ with connection:
|
|||||||
"name": "Le",
|
"name": "Le",
|
||||||
}
|
}
|
||||||
result = cursor.execute(sql, data2) # type: ignore
|
result = cursor.execute(sql, data2) # type: ignore
|
||||||
|
# print(sql)
|
||||||
|
# print(data2)
|
||||||
|
# print(result)
|
||||||
|
connection.commit()
|
||||||
|
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
sql = (
|
||||||
|
f'INSERT INTO {TABLE_NAME} '
|
||||||
|
'(nome, idade) '
|
||||||
|
'VALUES '
|
||||||
|
'(%(name)s, %(age)s) '
|
||||||
|
)
|
||||||
|
data3 = (
|
||||||
|
{"name": "Sah", "age": 33, },
|
||||||
|
{"name": "Júlia", "age": 74, },
|
||||||
|
{"name": "Rose", "age": 53, },
|
||||||
|
)
|
||||||
|
result = cursor.executemany(sql, data3) # type: ignore
|
||||||
|
# print(sql)
|
||||||
|
# print(data3)
|
||||||
|
# print(result)
|
||||||
|
connection.commit()
|
||||||
|
|
||||||
|
with connection.cursor() as cursor:
|
||||||
|
sql = (
|
||||||
|
f'INSERT INTO {TABLE_NAME} '
|
||||||
|
'(nome, idade) '
|
||||||
|
'VALUES '
|
||||||
|
'(%s, %s) '
|
||||||
|
)
|
||||||
|
data4 = (
|
||||||
|
("Siri", 22, ),
|
||||||
|
("Helena", 15, ),
|
||||||
|
)
|
||||||
|
result = cursor.executemany(sql, data4) # type: ignore
|
||||||
print(sql)
|
print(sql)
|
||||||
print(data2)
|
print(data4)
|
||||||
print(result)
|
print(result)
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
|||||||
Reference in New Issue
Block a user