[英]Having a connection issue with cx_oracle on a Flask app
Im trying to develop an app that gets a user input (an excel spreadsheet) and then upload that information to a database depending if the user pressed option A or option B.我正在尝试开发一个获取用户输入的应用程序(excel 电子表格),然后根据用户按下选项 A 或选项 B 将该信息上传到数据库。
The issue I´m having is that when I submit and upload something and try to then click the "go back" botton and try to upload someting else, the script gives me an error.我遇到的问题是,当我提交并上传某些内容并尝试单击“返回”按钮并尝试上传其他内容时,脚本会给我一个错误。
The backend is:后端是:
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
#Read the excel that was dragged
file = request.files['file']
#Identify the survey and project switchs
survey_switch = request.form.get("switch-1")
project_switch = request.form.get("switch-2")
#Convert the file dragged into a dataframe
df = pd.read_excel(file, skiprows=3, engine='openpyxl')
if survey_switch == 'Survey' and project_switch != 'Project':
session["survey_switch"] = survey_switch
session["project_switch"] = None
connection = utils.CreateConnection(file, df)
#Insert the Survey on the file dragged
utils.CreateSurvey(file, connection, df)
return redirect(url_for('messages'))
if project_switch == 'Project' and survey_switch != 'Survey':
session["survey_switch"] = None
session["project_switch"] = project_switch
connection = utils.CreateConnection(file, df)
#Insert the Project on the file dragged
utils.CreateProject(file, connection, df)
return redirect(url_for('messages'))
if survey_switch == 'Survey' and project_switch == 'Project':
session["survey_switch"] = survey_switch
session["project_switch"] = project_switch
connection = utils.CreateConnection(file, df)
#Insert the Survey and Project on the file dragged
utils.CreateSurvey(file, connection, df)
utils.CreateProject(file, connection, df)
return redirect(url_for('messages'))
return render_template('SIMMetadata_index.html')
#Make the messages template
@app.route('/UserInterface', methods=['GET', 'POST'])
def messages():
#Make the go back
if request.method == 'POST':
session["survey_switch"] = None
session["project_switch"] = None
return redirect(url_for('index'))
return render_template('UserInterfaceMessages.html')
The frontend are:前端是:
Index page索引页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11">
<title>SIM Metadata</title>
<link href="../static/css/em-unity-1.6.0.min.css" media="all" rel="stylesheet" />
<script type="text/javascript" src="../static/js/vendor/svg4everybody.min.js"></script><!-
- polyfill for SVG icons -->
</head>
<body>
<!-- Banner azul del head-->
<header class="em-c-header em-c-header--blue" role="banner">
<div class="em-l-container em-c-header__inner">
<div class="em-c-header__body">
<div class="em-c-header__title-container">
<h2 class="em-c-header__title"><a href="#" rel="home" class="em-c-header__title-
link">SIM Metadata</a></h2>
</div>
</div>
</header>
<!-- la caja de drag files con el padding respectivo-->
<div class="em-u-padding">
<!-- asignar distinta clase em-is-valid si el documento insertado es valido o em-has-error
si no es valido--->
<form action="/" method="POST" enctype="multipart/form-data" autocomplete="off">
<div class="em-c-field em-c-field--file-upload ">
<label for="file" class="em-c-field__label">KDM Spreadsheet</label>
<div class="em-c-field__body">
<svg class="em-c-icon em-c-icon--large em-c-field__block-icon">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../static/images/48/em-<body>
<!-- Banner azul del head-->
<header class="em-c-header em-c-header--blue" role="banner">
<div class="em-l-container em-c-header__inner">
<div class="em-c-header__body">
<div class="em-c-header__title-container">
<h2 class="em-c-header__title"><a href="#" rel="home" class="em-c-header__title-
link">SIM Metadata</a></h2>
</div>
</div>
</header>
<!-- la caja de drag files con el padding respectivo-->
<div class="em-u-padding">
<!-- asignar distinta clase em-is-valid si el documento insertado es valido o em-has-error
si no es valido--->
<form action="/" method="POST" enctype="multipart/form-data" autocomplete="off">
<div class="em-c-field em-c-field--file-upload ">
<label for="file" class="em-c-field__label">KDM Spreadsheet</label>
<div class="em-c-field__body">
<svg class="em-c-icon em-c-icon--large em-c-field__block-icon">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="../static/images/48/em-
icons.svg#upload"></use>
</svg>
<input type="file" name="file" id="file" class="em-c-file-upload"
placeholder="Placeholder" enctype="multipart/form-data" />
<ul class="em-c-field__list em-js-field-list">
<li class="em-c-field__item">Drag file here</li>
<li class="em-c-field__item em-c-field__item--small">Or click to choose file</li>
</ul>
</div>
<div class="em-c-field__note">The extension of the spreadsheet must be .xls or .xslx</div>
</div>
<ul class="em-c-switch-list">
<li class="em-c-switch-list__item">
<div class="em-c-field">
<div class="em-c-field__body">
<div class="em-c-switch">
<label class="em-c-switch__label" for="switch-1">
<span class="em-c-switch__toggle"></span>
<span class="em-c-switch__label-name">Upload Survey</span>
</label>
<input id="switch-1" type="checkbox" name="switch-1" value="Survey" class="em-c-
switch__input em-js-switch-trigger" >
</div>
</div>
</div>
</li>
<li class="em-c-switch-list__item">
<div class="em-c-field">
<div class="em-c-field__body">
<div class="em-c-switch">
<label class="em-c-switch__label" for="switch-2">
<span class="em-c-switch__toggle"></span>
<span class="em-c-switch__label-name">Upload Project</span>
</label>
<input id="switch-2" type="checkbox" name="switch-2" value="Project" class="em-c-
switch__input em-js-switch-trigger">
</div>
</div>
</div>
</li>
</ul>
<!-- agregar las botones de survey y project-->
<!--agregar el boton de submit -->
<div class="em-u-text-align-center">
<div class="em-c-btn-group "></div>
<button type="submit" value="submit" class="em-c-btn em-c-btn--primary">
<span class="em-c-btn__text">Submit</span>
</button>
</div>
</form>
<script type="text/javascript" src="../static/js/em-unity-1.6.0.min.js"></script>
</body>
Error I get我得到的错误
"Oracle client has already been initialized" “Oracle 客户端已经初始化”
"'None Type' object has no attribute 'cursor'" “‘无类型’object 没有属性‘光标’”
The first error suggests that you are calling cx_Oracle.init_oracle_client() more than one time.第一个错误表明您多次调用 cx_Oracle.init_oracle_client() 。 You aren't supposed to do that!
你不应该那样做! See the documentation for details.
有关详细信息,请参阅文档。 You can perform the initialization directly in your module code, or you can use a global variable to ensure that it is only called once.
您可以直接在模块代码中执行初始化,也可以使用全局变量来确保它只被调用一次。
The second error suggests that you are trying to create a cursor on a None
connection object -- which in turn suggests that your utils.CreateConnection()
function is catching exceptions and returning None
instead of a valid connection.第二个错误表明您正在尝试在
None
连接 object 上创建 cursor - 这反过来表明您的utils.CreateConnection()
function 正在返回None
而不是有效连接。 Avoid catching exceptions unless you have to, and then ensure that you're not just swallowing the exception and causing exceptions elsewhere!除非必须,否则避免捕获异常,然后确保您不只是吞下异常并在其他地方引发异常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.