forked from adgsenpai/pyodbc-heroku-config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
93 lines (70 loc) · 2.79 KB
/
Copy pathapp.py
File metadata and controls
93 lines (70 loc) · 2.79 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# ADGSTUDIOS 2021
import random, string
from flask import Flask, session
import pyodbc
# My custom sqlserver class / module which you can find @ pip install sqlserver
# This is modded version to work for Heroku drivers change conn with your connection strings
class sqlserver():
def __init__(self):
pass
def ExecuteQuery(self, Query):
try:
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=YOURSQLSERVERNAME,YOURPORTNUMBER;DATABASE=YOURDBNAME;UID=YOURUSERNAME;PWD=YOURPASSWORD')
cursor = conn.cursor()
cursor.execute(Query)
cursor.commit()
print("Query Executed")
except Exception as e:
print(e)
def fields(self, cur):
results = {}
column = 0
for d in cur.description:
results[d[0]] = column
column = column + 1
return results
def GetRecordsOfColumn(self, SelectQuery, ColumnName):
try:
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=YOURSQLSERVERNAME,YOURPORTNUMBER;DATABASE=YOURDBNAME;UID=YOURUSERNAME;PWD=YOURPASSWORD')
cursor = conn.cursor()
cursor.execute(SelectQuery)
field_map = self.fields(cursor)
values = []
for row in cursor:
values.append(row[field_map[ColumnName]])
return values
except Exception as e:
print(e)
# Flask Website Engine
app = Flask(__name__)
# Configuration for cookies
### Our Secrets
app.secret_key = ''.join(random.choices(string.ascii_letters + string.digits, k=16))
app.config['SESSION_TYPE'] = 'filesystem'
### Functions
def TestConnection():
bValid = False
db = sqlserver()
data = db.GetRecordsOfColumn('EXEC sp_helpdb','name')
if data is None:
bValid = False
else:
bValid = True
return bValid
# We work with our website here
@app.route('/')
def index():
#Clears cookies
session.clear()
for key in list(session.keys()):
session.pop(key)
if TestConnection() == True:
return '<h1>Database Connected Successfully :-) </h1><p>Congratulations you got your Flask/Heroku Application talking to SQL Server, Happy Hacking.</p>'
else:
return '<h1>Database Failed to Connect :-(</h1><p>Please look into the terminal for the error log if testing Localhost, else please check the Build Logs in Heroku or your connection string</p>'
##### if running under localhost enviroment testing grounds.
if __name__ == '__main__':
app.config['SESSION_TYPE'] = 'filesystem'
app.debug = True
app.run(host = 'localhost', port = 8000,debug = False)
# Author : Ashlin Darius Govindasamy