SELECT do SQL com fetch no SQLite3 do Python

This commit is contained in:
Luiz Otávio
2023-04-01 17:43:46 -03:00
parent e616b27e2f
commit f8eb69afad
2 changed files with 31 additions and 1 deletions

View File

@@ -51,7 +51,9 @@ cursor.executemany(sql, (
{'nome': 'Joana', 'peso': 5},
))
connection.commit()
print(sql)
cursor.close()
connection.close()
if __name__ == '__main__':
print(sql)

28
aula205/select.py Normal file
View File

@@ -0,0 +1,28 @@
import sqlite3
from main import DB_FILE, TABLE_NAME
connection = sqlite3.connect(DB_FILE)
cursor = connection.cursor()
cursor.execute(
f'SELECT * FROM {TABLE_NAME}'
)
for row in cursor.fetchall():
_id, name, weight = row
print(_id, name, weight)
print()
cursor.execute(
f'SELECT * FROM {TABLE_NAME} '
'WHERE id = "3"'
)
row = cursor.fetchone()
_id, name, weight = row
print(_id, name, weight)
cursor.close()
connection.close()