-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStore.py
69 lines (62 loc) · 1.81 KB
/
DataStore.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
67
68
#DBMS COnnector
from distutils.log import error
from genericpath import exists
from unittest import result
from xmlrpc.client import Boolean
import mysql.connector as DBconnector\
#Error's
import Error
try:
connector = DBconnector.connect(host="localhost", user="root", passwd = "Asdfgh@88", database = "URL")
cursor = connector.cursor()
except:
Error.databaseConnection()
'''
Checks if the generated shortURL, is already taken or not
If taken re-generares, else stores in shortURL, longURL -> store(longURL, shortURL)
@params -> shortURL -> String
'''
def isTaken(shortURL) -> Boolean:
try:
query = "select * from url_store where shortURL = (%s)";
value = [(shortURL)]
exists = cursor.execute(query, value)
return len(exists[0])
except:
return "Error : In is taken "
'''
@Params -> longURl, shortURL -> String
'''
def store(shortURL, longURL):
#Query for storing
try:
query = "insert into url_store(shortURL, longURL) values(%s,%s);"
values = (shortURL, longURL)
cursor.execute(query, values)
connector.commit()
except:
Error.databaseCommit()
'''
@params -> ShortUrl, give's the longURL
'''
def getlongUrl(shortUrl):
try:
query = "select longURL from url_store where shortURL = (%s);"
value = [(shortUrl)]
cursor.execute(query, value)
result = cursor.fetchone()
return result[0]
except:
return Error.databaseConnection()
''''
@params -> LongUrl, give's the shortURL
'''
def getshortUrl(longUrl):
try:
query = "select shortURL from url_store where longURL = (%s);"
value = [(longUrl)]
cursor.execute(query, value)
result = cursor.fetchone()
return result[0]
except:
return Error.databaseConnection()