forked from oli-cs/HackTheBurghX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
65 lines (56 loc) · 1.59 KB
/
database.py
File metadata and controls
65 lines (56 loc) · 1.59 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
53
54
55
56
57
58
59
60
61
62
63
64
65
import sqlite3
import os
global conn
global cursor
#connect to the database nand create a cursor object
def connect_db():
global conn
conn = sqlite3.connect('database.db', timeout=10)
global cursor
cursor = conn.cursor()
#create table
def create_db():
global cursor
cursor.execute('''CREATE TABLE "Houses" (
"id" INTEGER,
"agency" TEXT NOT NULL,
"beds" INTEGER NOT NULL,
"baths" INTEGER NOT NULL,
"price" INTEGER NOT NULL,
"address" TEXT NOT NULL,
"image" TEXT NOT NULL,
"desc" TEXT NOT NULL,
PRIMARY KEY("id")
)''')
#insert data into the table
def insert_into_db(id:int, agency:str, beds:int, baths:int, price:int, address:str, image:str, desc:str):
global cursor
cursor.execute('''INSERT INTO HOUSES VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', (id, agency, beds, baths, price, address, image, desc))
conn.commit()
#display all data
def display_db_data():
global cursor
data=cursor.execute('''SELECT * FROM HOUSES''')
for row in data:
print(row)
#get all the ids of houses
def get_ids():
global cursor
id = []
data=cursor.execute('''SELECT ID FROM HOUSES''')
for row in data:
id.append(row[0])
return id
#get a row corresponding to a specified house ID
def get_info(id:int):
global cursor
data=cursor.execute('''SELECT * FROM HOUSES WHERE ID = (?)''', [id])
for row in data:
return row
#delete the entire database
def delete_db():
os.remove('''database.db''')
#close the connection
def close_db():
global conn
conn.close()