forked from IndianHills-CSD/Amazon-2-Electric-Boogaloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbHelper.py
87 lines (73 loc) · 3.46 KB
/
dbHelper.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import pyodbc
from Amazon2 import dbModels
class dbHelper:
#Class used to read and write to sql server database.
#Adds new user with supplied properties
def addNewUser(email,username,password):
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=(localdb)\MSSQLLocalDB;'
'Database=Backroom;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
count = cursor.execute('''Select Count(EmailID) FROM Backroom.dbo.Users WHERE EmailID = ?''',email)
if count.arraysize > 0:
return 1
cursor.execute('''INSERT into Backroom.dbo.Users VALUES(?,?,?)''',email,username,password)
conn.commit()
return 0
#Returns true or false based on if supplied args match db properties
def login(name,pword):
logged_in = False
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=(localdb)\MSSQLLocalDB;'
'Database=Backroom;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('''SELECT * FROM Backroom.dbo.Users WHERE UserName =? AND Password =?''',name,pword)
for row in cursor:
logged_in = True
return logged_in
#Adds new item with supplied arguments
def addItem(auth,title,desc,loc,price):
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=(localdb)\MSSQLLocalDB;'
'Database=Backroom;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('''INSERT into Backroom.dbo.Post(Author,Title,"Desc","Location",Price) VALUES(?,?,?,?,?)''',auth,title,desc,loc,price)
conn.commit()
#Returns single post item based on id
def getItemById(ID):
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=(localdb)\MSSQLLocalDB;'
'Database=Backroom;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('SELECT * FROM Backroom.dbo.Post WHERE PostID =?',ID)
for row in cursor:
post = postModel(row[0],row[1],row[2],row[3],row[4],row[5])
return post
#Deletes item by id
def deleteItemById(ID):
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=(localdb)\MSSQLLocalDB;'
'Database=Backroom;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('''DELETE FROM Backroom.dbo.Post WHERE PostID=?''',ID)
conn.commit()
#Returns list of all posts
def getAllItems():
conn = pyodbc.connect('Driver={SQL Server Native Client 11.0};'
'Server=(localdb)\MSSQLLocalDB;'
'Database=Backroom;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('SELECT * FROM Backroom.dbo.Post')
posts = []
for row in cursor:
post = postModel(row[0],row[1],row[2],row[3],row[4],row[5])
posts.append(post)
return posts
#method to get all current categories
#method to join post with new catagories table