[英]Strange SQL error Python Flask -Multiple connections to DB
使用多個連接到同一數據庫時遇到一個奇怪的錯誤。 我的應用程序接收一個.csv文件並創建2個sql表,並允許用戶從該數據集中收藏項。
問題:當我僅使用一個連接進行數據幀上傳時,則可以執行(csv-df-sql)。 但是,當我使用“收藏夾”中當前使用的另一個連接時,則無法執行df到sql。
如果取消注釋#cnx3.close(),則一切正常,但是我無法再獲取收藏夾所需的數據。
相同的數據源用於用戶收藏夾。 我已經找到了一個臨時的解決方法,但是我想要一些有關如何解決此問題的指導,以便可以使用應用程序的兩個元素。
相關代碼:
進口:
# Imported Modules
from flask import Flask, render_template, flash, redirect, url_for, session, logging, request, jsonify
from flask_wtf import RecaptchaField
from wtforms import Form,StringField, TextAreaField, PasswordField, validators
from functools import wraps
#### SQL connection
from sqlalchemy import create_engine
## Useful - Needed for Python 3 as MySQLDB Does Not Support This !
import pymysql
pymysql.install_as_MySQLdb()
import numpy as np
import json
from werkzeug import secure_filename
import pandas as pd
import tempfile
import mysql.connector
SQL連接器:
config3 = {
'user':'root',
'password':'',
'host':'localhost',
'raise_on_warnings':True,
}
cnx = mysql.connector.connect(**config)
cnx2 = mysql.connector.connect(**config2)
cnx3 = mysql.connector.connect(**config3)
engine = create_engine('mysql://root:@localhost/tableau_data?charset=utf8' ,encoding='utf-8')
數據框上傳:
@app.route('/upload', methods =['GET', 'POST'])
@auth
def csv_input():
tempfile_path = tempfile.NamedTemporaryFile().name
#file.save(tempfile_path)
#sheet = pd.read_csv(tempfile_path)
if request.method == 'POST':
file = request.files['file']
if file:
try:
#allowed_filename(file.filename):
#filename = secure_filename(file.filename)
file.save(tempfile_path)
input_csv = pd.read_csv(tempfile_path,sep=",", engine='python')
#### Data Cleansing From Uploded Data
col_titles = ['id','title','vote_average','w_average','vote_count','year','runtime',
'budget','revenue','profit']
# Only Keep Data where the Original Language is English
input_csv = input_csv[input_csv['original_language']=='en']
# New Dataframe that only contains data with vote count > 10
input_csv = input_csv[input_csv['vote_count'] >= 10]
# Fill all NA values to 0 - Needed to set datatypes
input_csv = input_csv.fillna(0)
# Remove all Rows with no Runtime
input_csv = input_csv[input_csv['runtime']!=0]
# Revmove all duplciate Rows
input_csv = input_csv.drop_duplicates()
input_csv['vote_average'] = input_csv.vote_average.astype(float).round(1)
input_csv.vote_average.round(1)
input_csv['runtime'] = input_csv.runtime.astype(int)
input_csv['vote_count'] = input_csv.vote_count.astype(int)
input_csv['revenue'] = input_csv.revenue.astype('int64')
input_csv['budget'] = input_csv.budget.astype('int64')
profit_cal(input_csv,'revenue','budget','profit')
input_csv['profit']=input_csv.profit.astype('int64')
input_csv['profit']=input_csv.profit.replace(0,'No Data')
#reorder_data = pd.DataFrame(input_csv)
# Year Cleaning
input_csv['year'] = pd.to_datetime(input_csv['release_date'], errors='coerce').apply(lambda x: str(x).split('-')[0] if x != np.nan else np.nan)
#C = reorder_data['vote_average'].mean()
#m = reorder_data['vote_count'].quantile(0.10)
#w_average = org_data.copy().loc[reorder_data['vote_count'] >= m]
#### IMDB Data Calculation
V = input_csv['vote_count']
R = input_csv['vote_average']
C = input_csv['vote_average'].mean()
m = input_csv['vote_count'].quantile(0.10)
input_csv['w_average'] = (V/(V+m) * R) + (m/(m+V) * C)
input_csv = input_csv[input_csv['vote_count'] >m]
#C = input_csv['vote_average'].mean()
#m = input_csv['vote_count'].quantile(0.10)
#input_csv['w_average'] = input_csv.apply(weighted_rating, axis = 1)
input_csv['w_average'] = input_csv.w_average.astype(float).round(1)
#cursor = cnx3.cursor(dictionary=True,buffered=True)
#cnx3.close()
reorder_data = input_csv[col_titles]
reorder_data.to_sql(name='title_data', con=engine, if_exists = 'replace', index=False)
# Reorder the data and output in the correct order
##### Genre Loads == DataFrame 2
df = input_csv
v = df.genres.apply(json.loads)
df = pd.DataFrame(
{
'id' : df['id'].values.repeat(v.str.len(), axis=0),
'genre' : np.concatenate(v.tolist())
})
df['genre'] = df['genre'].map(lambda x: x.get('name'))
genre_data = df.genre.str.get_dummies().sum(level=0)
genre_data = df.loc[(df!=0).any(1)]
#genre_data = genre_data.set_index('id')
genre_order = ['id','genre']
## Dataframw to SQL
genre_data[genre_order].to_sql(name='genre_data', con=engine, if_exists = 'replace', index=False)
####### Keyword Search ### Dataframe
#genre_data.to_csv("genre_data.csv")
#return genre_data[genre_order].to_html()
flash('Database has been updated successfully','success')
#return reorder_data[col_titles].to_html()
#stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None)
#csv_input = csv.reader(stream)
#return reorder_data.to_html(index=False)
#flash('File Uploaded Successfully')
#return redirect(url_for('index'))
except pd.errors.EmptyDataError as ex:
flash('No File Selected','danger')
except pd.errors.ParserError as ex:
flash('Invalid File Format','danger')
except Exception as ex:
flash('Invalid File Format','danger')
return render_template('upload.html')
收藏夾
@app.route('/my_f')
def my_f():
# Create Cursor
cursor = cnx3.cursor(dictionary=True)
cursor.execute("SELECT favourites.id,favourites.rating,title_data.title,title_data.w_average,title_data.runtime,title_data.vote_count,title_data.year from tableau_data.title_data inner join webapp.favourites on webapp.favourites.film_id = tableau_data.title_data.id WHERE webapp.favourites.username = %s",([session['username']]))
## Fetch all Results - Need to figure out why this is not displaying
results = cursor.fetchall()
if results is not None:
flash('Data Found','success')
cursor.close()
#cnx3.close()
#Here I can close this connection, but then this function does not work
return render_template('my_f.html', results=results)
#cnx3.close() #/// Need to sort out the dual connection - When Updating the data
else:
# Message if the sql query does not return a value
flash('Nothing Found', 'danger')
return render_template('my_f.html')
return render_template('my_f.html')
考慮在每種方法中而不是在全局范圍內處理打開/關閉連接,這可能會使連接保持打開狀態,例如在pandas操作中使用的sqlAlchemy引擎。 此外,對於HTML模板渲染,由於空的光標提取將返回空列表而不是None
,因此有條件地將結果重新分配為實際的None
。
數據框操作
@app.route('/upload', methods =['GET', 'POST'])
@auth
def csv_input():
# ... same code in method
# OPEN ENGINE
engine = create_engine('mysql://root:@localhost/tableau_data?charset=utf8', encoding='utf-8')
# RUN REPLACE AND APPEND
reorder_data.to_sql(name='title_data', con=engine, if_exists = 'replace', index=False)
# CLOSE ENGINE
engine.dispose()
# ... same code in method
模板渲染
@app.route('/my_f')
def my_f():
# OPEN CONNECTION
cnx3 = pymysql.connect(***)
# Create Cursor
my_cursor = cnx3.cursor(dictionary=True)
sql = """SELECT f.id, f.rating, t.title, t.w_average, t.runtime,
t.vote_count, t.year
FROM tableau_data.title_data t
INNER JOIN webapp.favourites f on f.film_id = t.id
WHERE f.username = %s"""
my_cursor.execute(sql, (session['username'],))
## Fetch all results into local list
results = my_cursor.fetchall()
# CLOSE CURSOR AND CONNECTION
my_cursor.close()
cnx3.close()
if len(results) > 0:
flash('Data Found', 'success')
else:
# Message if the sql query does not return a value
flash('Nothing Found', 'danger')
results = None
return render_template('my_f.html', results=results)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.