Apagando valores com DELETE, WHERE e placeholders no PyMySQL

This commit is contained in:
Luiz Otávio
2023-04-15 17:21:53 -03:00
parent c0c3e77853
commit 6c70e5c143

View File

@@ -107,8 +107,10 @@ with connection:
# Lendo os valores com SELECT # Lendo os valores com SELECT
with connection.cursor() as cursor: with connection.cursor() as cursor:
menor_id = int(input('Digite o menor id: ')) # menor_id = int(input('Digite o menor id: '))
maior_id = int(input('Digite o maior id: ')) # maior_id = int(input('Digite o maior id: '))
menor_id = 2
maior_id = 4
sql = ( sql = (
f'SELECT * FROM {TABLE_NAME} ' f'SELECT * FROM {TABLE_NAME} '
@@ -116,8 +118,22 @@ with connection:
) )
cursor.execute(sql, (menor_id, maior_id)) # type: ignore cursor.execute(sql, (menor_id, maior_id)) # type: ignore
print(cursor.mogrify(sql, (menor_id, maior_id))) # type: ignore # print(cursor.mogrify(sql, (menor_id, maior_id))) # type: ignore
data5 = cursor.fetchall() # type: ignore data5 = cursor.fetchall() # type: ignore
for row in data5: # for row in data5:
# print(row)
# Apagando com DELETE, WHERE e placeholders no PyMySQL
with connection.cursor() as cursor:
sql = (
f'DELETE FROM {TABLE_NAME} '
'WHERE id = %s'
)
print(cursor.execute(sql, (1,))) # type: ignore
connection.commit()
cursor.execute(f'SELECT * FROM {TABLE_NAME} ') # type: ignore
for row in cursor.fetchall(): # type: ignore
print(row) print(row)