【问题标题】:Having a connection issue with cx_oracle on a Flask app在 Flask 应用程序上遇到 cx_oracle 连接问题
【发布时间】:2021-08-27 13:57:42
【问题描述】:

我正在尝试开发一个应用程序来获取用户输入(一个 Excel 电子表格),然后根据用户按下选项 A 还是选项 B,将该信息上传到数据库。

我遇到的问题是,当我提交并上传某些内容并尝试单击“返回”按钮并尝试上传其他内容时,脚本给了我一个错误。

后端是:

@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')

前端是:

索引页面

<!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>

我得到的错误

“Oracle 客户端已经初始化”

"'None Type'对象没有属性'cursor'"

【问题讨论】:

  • 您有 2 个不同的错误。错误 1 ​​将客户端初始化两次。错误 2 在未初始化的对象上使用游标。您确定每次都必须创建“连接”吗?
  • 我尝试在第一个 if 之前只执行 1 "connection = utils.CreateConnection(file, df)" 但它也不起作用

标签: python html flask web cx-oracle


【解决方案1】:

第一个错误表明您多次调用 cx_Oracle.init_oracle_client()。你不应该那样做!有关详细信息,请参阅documentation。您可以直接在模块代码中执行初始化,也可以使用全局变量来确保它只被调用一次。

第二个错误表明您正在尝试在 None 连接对象上创建游标 - 这反过来表明您的 utils.CreateConnection() 函数正在捕获异常并返回 None 而不是有效连接。除非必须,否则避免捕获异常,然后确保您不只是吞下异常并在其他地方引发异常!

【讨论】:

    猜你喜欢
    • 2017-01-18
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2021-04-14
    相关资源
    最近更新 更多