-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA5T8SQLite.py
66 lines (56 loc) · 1.54 KB
/
A5T8SQLite.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import sqlite3
import csv
import random
import time
connection = None
cursor = None
def connect(path):
global connection, cursor
connection = sqlite3.connect(path)
cursor = connection.cursor()
cursor.execute(' PRAGMA forteign_keys=ON; ')
connection.commit()
def find_listing_id():
global connection
global cursor
number = input("Please Enter a listing_id you are looking for:")
if not number.isdigit():
return -1
number = int(number)
cursor.execute('''
SElECT listing_id
From reviews
''')
rows = cursor.fetchall()
for row in rows:
if row[0] == number:
return number
return -1
def query(select_id):
global connection
global cursor
t1 = time.time()
cursor.execute('''
SELECT host_name, price, comments
FROM reviews R,listings S
WHERE R.listing_id = :ID
AND R.listing_id = S.id
ORDER BY date DESC
LIMIT 1
''',{'ID':select_id})
t2 = time.time()
rows = cursor.fetchall()
for row in rows:
print("Host name:",row[0],"\nPrice:",row[1],"\nLatest review:", row[2])
print("Running time is: " + str((t2-t1)*1000) + "ms.")
def main():
path = "./A5.db"
connect(path)
select_id = find_listing_id()
if select_id != -1:
query(select_id)
else:
print("No such id")
connection.commit()
connection.close()
main()