Код: Выделить всё
def get_sites(self):
query = "SELECT * FROM some_table"
cursor = self.conn.cursor(dictionary=True)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
return results
def get_assets(self):
query = "SELECT * FROM some_table"
cursor = self.conn.cursor(dictionary=True)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
return results
Вот как я устанавливаю и сохраняю SQL-соединения для использования в верхней части моего приложения Flask:
Код: Выделить всё
CUSTOMER_DBS = {}
customer_db_routing_config = configparser.ConfigParser()
customer_db_routing_config.optionxform = str
customer_db_routing_config.read_file(open("customer_db_routing.ini"))
for token in customer_db_routing_config['customers']:
customer_db_config = configparser.ConfigParser()
customer_db_config.read_file(open(customer_db_routing_config['customers'][token]))
customer_db_conn = DatabaseInterface(customer_db_config)
CUSTOMER_DBS[token] = customer_db_conn
Код: Выделить всё
def __init__(self, config):
self.config = config
self.conn = mysql.connector.connect(user=config['mysql']['user'],
password=config['mysql']['password'],
host=config['mysql']['host'])
cursor = self.conn.cursor()
cursor.execute("SET SESSION TRANSACTION READ ONLY")
Вот два маршрута, на которых возникает проблема:
Код: Выделить всё
@app.route("/api/customer//site")
@key_required
def sites(key, token):
if token in FILTERED_TOKENS:
return {"error": f"No valid customer with token {token}" }
site_name = request.args.get("name")
site_type = request.args.get("type")
if len(request.args) > 2:
return {"error": "Only ONE of the following parameters may be used: name, type"}
# use customer token to get path to customer db config
if not CUSTOMER_DBS[token].conn.is_connected():
CUSTOMER_DBS[token].conn.reconnect()
# get sites from customer db
if len(request.args) == 1:
results = CUSTOMER_DBS[token].get_sites()
elif site_name:
results = CUSTOMER_DBS[token].get_site_by_name(site_name)
elif site_type:
results = CUSTOMER_DBS[token].get_sites_by_type(site_type)
# base64 encode logo png and info_json so they are json serializable
for row in results:
if row["logo"]:
row["logo"] = base64.b64encode(row["logo"]).decode("utf-8")
# this column is oddly missing from
if "info_json" in row.keys():
if row["info_json"]:
row["info_json"] = base64.b64encode(row["info_json"]).decode("utf-8")
return results
@app.route("/api/customer//asset")
@key_required
def assets(key, token):
if token in FILTERED_TOKENS:
return {"error": f"No valid customer with token {token}"}
asset_name = request.args.get("name")
site_name = request.args.get("site_name")
site_id = request.args.get("site_id")
superasset_id = request.args.get("superasset_id")
superasset_name = request.args.get("superasset_name")
address = request.args.get("address")
if len(request.args) > 2:
return {"error": "Only ONE of the following parameters may be used: name, site_name, site_id, superasset_id, superasset_name, address"}
# use customer token to get path to customer db config
if not CUSTOMER_DBS[token].conn.is_connected():
CUSTOMER_DBS[token].conn.reconnect()
# get assets from customer db
if len(request.args) == 1:
results = CUSTOMER_DBS[token].get_assets()
elif asset_name:
results = CUSTOMER_DBS[token].get_asset_by_name(asset_name)
elif site_name:
results = CUSTOMER_DBS[token].get_assets_by_site_name(site_name)
elif site_id:
results = CUSTOMER_DBS[token].get_assets_by_site_id(site_id)
elif superasset_id:
results = CUSTOMER_DBS[token].get_assets_by_superasset_id(superasset_id)
elif superasset_name:
results = CUSTOMER_DBS[token].get_assets_by_superasset_name(superasset_name)
elif address:
results = CUSTOMER_DBS[token].get_assets_by_address(address)
# base64 encoding blobs to avoid errors when returning
for row in results:
if row["info_json"]:
row["info_json"] = base64.b64encode(row["info_json"]).decode("utf-8")
if row["rt_condition_json"]:
row["rt_condition_json"] = base64.b64encode(row["rt_condition_json"]).decode("utf-8")
# filter out NAA
results = [ row for row in results if row["asset_tag"] != "NAA" ]
return results
Подробнее здесь: https://stackoverflow.com/questions/786 ... or-execute