/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
custom_with__context_manager/sqlite_execute.py
92 строки
2 KB
gil9red
Refactoring. Using black code style
27 апр 2023, 12:07
27 апр 2023, 12:07
9107f3f
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" import sqlite3 def old_old_variant(): connect = sqlite3.connect(":memory:") try: print(connect.execute("SELECT sqlite_version();").fetchone()) connect.executescript("""\ CREATE TABLE Test ( id INTEGER PRIMARY KEY, name TEXT ); INSERT INTO Test (name) VALUES ('One'); INSERT INTO Test (name) VALUES ('Two'); INSERT INTO Test (name) VALUES ('Three'); """) print(connect.execute("select * from Test;").fetchall()) print() connect.commit() finally: connect.close() def old_variant(): with sqlite3.connect(":memory:") as connect: print(connect.execute("SELECT sqlite_version();").fetchone()) connect.executescript("""\ CREATE TABLE Test ( id INTEGER PRIMARY KEY, name TEXT ); INSERT INTO Test (name) VALUES ('One'); INSERT INTO Test (name) VALUES ('Two'); INSERT INTO Test (name) VALUES ('Three'); """) print(connect.execute("select * from Test;").fetchall()) print() connect.commit() class SQLite3Connect(object): def __init__(self, database): self._connect = sqlite3.connect(database) def __enter__(self): return self._connect def __exit__(self, exc_type, exc_value, exc_traceback): self._connect.close() def new_variant(): with SQLite3Connect(":memory:") as connect: print(connect.execute("SELECT sqlite_version();").fetchone()) connect.executescript("""\ CREATE TABLE Test ( id INTEGER PRIMARY KEY, name TEXT ); INSERT INTO Test (name) VALUES ('One'); INSERT INTO Test (name) VALUES ('Two'); INSERT INTO Test (name) VALUES ('Three'); """) print(connect.execute("select * from Test;").fetchall()) print() connect.commit() if __name__ == "__main__": old_old_variant() old_variant() new_variant()