-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.py
57 lines (49 loc) · 1.94 KB
/
sql.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
def sqlalchemy_db_test_html(environ,app):
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import text
import pymysql
DB_CONFIG = {}
DB_CONFIG["ENGINE"] = environ.get("DB_ENGINE", "")
DB_CONFIG["HOSTNAME"] = environ.get("DB_HOSTNAME", "")
DB_CONFIG["PORT"] = environ.get("DB_PORT", "")
DB_CONFIG["NAME"] = environ.get("DB_NAME", "")
DB_CONFIG["USERNAME"] = environ.get("DB_USERNAME", "")[1:-1]
DB_CONFIG["PASSWORD"] = environ.get("DB_PASSWORD", "")[1:-1]
if DB_CONFIG["ENGINE"] == "mysql":
DB_CONFIG["DIALECT_DRIVER"] = "mysql+pymysql"
elif DB_CONFIG["ENGINE"] == "postgresql":
DB_CONFIG["DIALECT_DRIVER"] = "???"
else:
DB_CONFIG["DIALECT_DRIVER"] = "???"
DB_CONFIG["URI"] = DB_CONFIG["DIALECT_DRIVER"] + '://' + DB_CONFIG["USERNAME"] + ':' + DB_CONFIG["PASSWORD"] + '@' + DB_CONFIG["HOSTNAME"] + ':' + DB_CONFIG["PORT"] + '/' + DB_CONFIG["NAME"]
html = ""
html += "SQLALCHEMY CONFIG AND DB OBJECT CREATION <br/> \n"
html += "---------------------------------------- <br/> \n"
app.config['SQLALCHEMY_DATABASE_URI'] = DB_CONFIG["URI"]
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = 'False'
## DON'T DO THIS - CAUSES db.engine.execute ERROR: 'True'
#app.config['SQLALCHEMY_ECHO'] = 'True'
try:
db = SQLAlchemy(app)
html += '--> SUCCESS! <br/> \n'
except Exception as e:
html += '--> ERROR: ' + str(e) + ' <br/> \n'
html += "<br/> \n"
html += "TEST DB CONNECTION <br/> \n"
html += "------------------ <br/> \n"
try:
db.engine.execute(text("SELECT 1"))
html += '--> SUCCESS! <br/> \n'
except Exception as e:
html += '--> ERROR: ' + str(e) + ' <br/> \n'
html += "<br/> \n"
html += "LIST DB TABLES <br/> \n"
html += "------------------ <br/> \n"
try:
tables = db.engine.table_names()
html += "SUCCESS! DB TABLES:"
html += str(tables)
except Exception as e:
tables = "FAILED" + str(e) + ' <br/> \n'
html += "<br/> \n"
return html