· 6 years ago · Apr 17, 2019, 07:50 AM
1import pprint
2import sqlite3
3conn = sqlite3.connect('BD_BOOKS1.sqlite')
4c = conn.cursor()
5pp = pprint.PrettyPrinter(width=120)
6
7#c.execute('''drop table A''')
8
9#c.execute('''drop trigger ins_items''')
10
11#c.execute('''drop trigger del_items''')
12
13#c.execute('''drop trigger upd_items''')
14
15c.execute('''CREATE TABLE if not exists a (
16A_Id_old INTEGER,
17A_Id_new INTEGER,
18A_contract_old INTEGER,
19A_contract_new INTEGER,
20A_n_old INTEGER,
21A_n_new INTEGER,
22A_date TEXT NOT NULL,
23operation TEXT NOT NULL);
24''')
25
26
27c.execute(''' create trigger if not exists ins_items
28after insert on items begin
29insert into a values (null, new.i_id, null, new.i_contract, null, new.i_count, datetime('now'), 'insert');
30end
31''')
32
33
34items = [(1,3,30), (10,13,2)]
35#c.executemany('''INSERT INTO items VALUES(?,?,?)''',items)
36conn.commit()
37c.execute('''
38
39''')
40
41c.execute('''
42select *
43from items ''')
44pp.pprint(c.fetchall())
45
46c.execute('''
47select *
48from A ''')
49pp.pprint(c.fetchall())
50
51
52
53c.execute(''' create trigger if not exists del_items
54after delete on items begin
55insert into a values (old.i_id, null, old.i_contract, null, old.i_count, datetime('now'), 'delete');
56end
57''')
58
59
60c.execute(''' create trigger if not exists upd_items
61after update on items begin
62insert into a values (old.i_id,new.i_id,old.i_contract,new.i_contract, old.i_count,new.i_count,datetime('now'), 'update');
63end
64''')
65
66conn.close()