/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
sqlite3__examples/e1.py
51 строка
1 KB
gil9red
Bugfix
18 сен 2024, 00:00
18 сен 2024, 00:00
62e9b59
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" # https://docs.python.org/3.4/library/sqlite3.html import sqlite3 from pathlib import Path DIR = Path(__file__).parent FILE_NAME = Path(__file__).resolve() conn = sqlite3.connect(str(DIR / FILE_NAME.stem) + ".db") c = conn.cursor() # Create table c.execute( """CREATE TABLE IF NOT EXISTS stocks (date text, trans text, symbol text, qty real, price real)""" ) # Insert a row of data c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)") # Save (commit) the changes conn.commit() # Do this instead t = ("RHAT",) c.execute("SELECT * FROM stocks WHERE symbol=?", t) print(c.fetchone()) # Larger example that inserts many records at a time purchases = [ ("2006-03-28", "BUY", "IBM", 1000, 45.00), ("2006-04-05", "BUY", "MSFT", 1000, 72.00), ("2006-04-06", "SELL", "IBM", 500, 53.00), ] c.executemany("INSERT INTO stocks VALUES (?,?,?,?,?)", purchases) # Save (commit) the changes conn.commit() for row in c.execute("SELECT * FROM stocks ORDER BY price"): print(row) # We can also close the connection if we are done with it. # Just be sure any changes have been committed or they will be lost. conn.close()