Mazání dat z databáze

Introduction to Databases in Python

Jason Myers

Co-Author of Essential SQLAlchemy and Software Engineer

Mazání dat z tabulky

  • Provádí se příkazem delete()
  • delete() přijímá jako argument tabulku, ze které se data odstraňují
  • Klauzule where() určuje, které řádky se smažou
  • Obtížně se vrátí zpět – buďte opatrní!
Introduction to Databases in Python

Smazání všech dat z tabulky

from sqlalchemy import delete

stmt = select([func.count(extra_employees.columns.id)])
connection.execute(stmt).scalar()
3
delete_stmt = delete(extra_employees)

result_proxy = connection.execute(delete_stmt) result_proxy.rowcount
3
Introduction to Databases in Python

Mazání konkrétních řádků

  • Sestavte klauzuli where(), která vybere záznamy určené k smazání
Introduction to Databases in Python

Mazání konkrétních řádků

stmt = delete(employees).where(employees.columns.id == 3)

result_proxy = connection.execute(stmt) result_proxy.rowcount
1
Introduction to Databases in Python

Úplné odstranění tabulky

  • Používá metodu drop() na tabulce
  • Přijímá engine jako argument, aby věděl, odkud tabulku odstranit
  • Z metadat ji odstraní až po restartu procesu Python
Introduction to Databases in Python

Odstranění tabulky

extra_employees.drop(engine)

print(extra_employees.exists(engine))
False
Introduction to Databases in Python

Odstranění všech tabulek

  • Používá metodu drop_all() na objektu MetaData
Introduction to Databases in Python

Odstranění všech tabulek

metadata.drop_all(engine)

engine.table_names()
[]
Introduction to Databases in Python

Vamos praticar!

Introduction to Databases in Python

Preparing Video For Download...