From 6c70e5c143ae50da4bda0d7000a44d0ea5e8457a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio?= Date: Sat, 15 Apr 2023 17:21:53 -0300 Subject: [PATCH] Apagando valores com DELETE, WHERE e placeholders no PyMySQL --- aula206/main.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/aula206/main.py b/aula206/main.py index 0e43159..9562dfb 100644 --- a/aula206/main.py +++ b/aula206/main.py @@ -107,8 +107,10 @@ with connection: # Lendo os valores com SELECT with connection.cursor() as cursor: - menor_id = int(input('Digite o menor id: ')) - maior_id = int(input('Digite o maior id: ')) + # menor_id = int(input('Digite o menor id: ')) + # maior_id = int(input('Digite o maior id: ')) + menor_id = 2 + maior_id = 4 sql = ( f'SELECT * FROM {TABLE_NAME} ' @@ -116,8 +118,22 @@ with connection: ) 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 - 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)