我是python的新手,我通过尝试将连接变量分配给不同的.py文件中的函数而得到上述错误
我试图将连接设置为无,但没有解决我的问题
视图.py
from datetime import datetime
from flask import render_template, request, redirect, url_for, flash
from FlaskWebProject1 import app
import json
import mysql.connector
from mysql.connector import Error
import FlaskWebProject1.db
@app.route('/')
@app.route('/users')
def users():
try:
lstUsers = ''
#connect to DB
connection = db.get_connection()
if connection.is_connected():
#db_Info = connection.get_server_info()
#print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor(dictionary=True)
cursor.execute("select * from users;")
record = cursor.fetchall()
lstUsers = record
#print("Your connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
return render_template('users.html',
title = 'Users Page',
users = lstUsers)
数据库.py
_connection = None
def get_connection():
global _connection
if not _connection:
_connection = mysql.connector.connect(user="bla@bla",
password='bla',
host="mysql.database.azure.com",
port=3306,
database='testdb')
return _connection
# List of stuff accessible to importers of this module. Just in case
__all__ = [ 'getConnection' ]
MM们
相关分类