-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
46 lines (37 loc) · 1.09 KB
/
database.py
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
import sqlite3
DB_NAME = 'database/database.sqlite3'
conn = sqlite3.connect(DB_NAME)
conn.cursor().execute('''
CREATE TABLE IF NOT EXISTS users
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
name TEXT NOT NULL,
address TEXT NOT NULL,
telephone TEXT NOT NULL
)
''')
conn.commit()
conn.cursor().execute('''
CREATE TABLE IF NOT EXISTS ads
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
description TEXT NOT NULL,
price TEXT NOT NULL,
date_created TEXT NOT NULL,
is_active INTEGER NOT NULL,
owner_id INTEGER,
buyer_id INTEGER,
FOREIGN KEY(owner_id) REFERENCES users(id),
FOREIGN KEY(buyer_id) REFERENCES users(id)
)
''')
conn.commit()
class DB:
def __enter__(self):
self.conn = sqlite3.connect(DB_NAME)
return self.conn.cursor()
def __exit__(self, type, value, traceback):
self.conn.commit()