forked from GeekBrainsTutorial/Python_lessons_basic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7_sqlite.py
More file actions
63 lines (43 loc) · 1.61 KB
/
7_sqlite.py
File metadata and controls
63 lines (43 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#
import os
import sqlite3
import datetime
# Создание файла базы данных
db_filename = 'todo.db'
conn = sqlite3.connect(db_filename)
conn.close()
# Создание схемы
# Схема определяет таблицы в базе данных
os.remove(db_filename)
with sqlite3.connect(db_filename) as conn:
conn.execute("""
create table project (
name text primary key,
description text,
deadline date
);
""")
# Insert
for i in range(10):
conn.execute("""
insert into project (name, description, deadline) VALUES (?,?,?)""", (
'project %s'%i,
'project %s description'%i,
datetime.date.today()+datetime.timedelta(days=i**2)
)
)
with sqlite3.connect(db_filename) as conn:
# Select
# Объекты connection имеют атрибут row_factory, который позволяет вызывать
# код, контролирующий тип объкта, создаваемого для каждой строки в запросе
# Объекты Row дают доступ к данным по индексу и имени
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute("select * from project")
for row in cur.fetchall():
print(row)
name, description, deadline = row
print(name, description, deadline)
# Update
cur.execute("update project set deadline=:deadline where name=:name",
{'deadline': '2013-09-15', 'name': 'project 0'})