在DB中进行某些更改后,Flask Server是否需要重新启动?

时间:2019-03-18 07:09:09

标签: python flask

我正在使用Flask创建Web服务。当我对DB进行一些更改时,我需要重新启动Flask服务器。

db = pymysql.connect("localhost","root","","posdata")
db1 = pymysql.connect("localhost","root","","posdata")

我还必须创建两个数据库连接才能使某些功能正常工作。如果我仅使用一个连接(上面说db),我会得到

`InterfaceError (0, '')`

因此要处理InterfaceError,我创建了两个数据库连接。但是现在的问题是,当我对数据库进行一些更改时,数据库工作正常,但db1无法正常工作,我需要重新启动flask服务器以使db1连接正常工作。 / p>

@app.route('/data_url/<customer_name>')
def data(customer_name):
    cursor = db.cursor()
    cursor.execute(" select country_name from countries as c, customers as cu where c.c_id = cu.c_id and cu.customer_id = '%s' " % customer_name)
    data =  cursor.fetchall()
    data = [x[0] for x in data]
    return jsonify(data)


**# query to select TYPE attribute from DB and send it to ajax**

@app.route('/url_for/<customer_name>')
def dt(customer_name):
    cursor = db1.cursor()
    cursor.execute(" select type from customers where customer_id = '%s' " % customer_name)
    data =  cursor.fetchall()
    data = [x[0] for x in data]
    return jsonify(data)

要使 @ app.route('/ url_for /')工作,我需要重新启动Flask服务器。

使用单个数据库连接时的错误回溯

Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\OneDrive -\Desktop\Project\TestApp\app.py", line 34, in Request
    cursor.execute('select customer_id from customers')
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 170, in execute
    result = self._query(query)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 328, in _query
    conn.query(q)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 516, in query
    self._execute_command(COMMAND.COM_QUERY, sql)
  File "C:\Users\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 750, in _execute_command
    raise err.InterfaceError("(0, '')")
pymysql.err.InterfaceError: (0, '')

2 个答案:

答案 0 :(得分:0)

您已更改了之前的代码,但它并不反映您当前正在运行的 Flask Server 。答案是yes,为了反映对服务器的新修改,您需要重新启动Flask Server

答案 1 :(得分:0)

是的。每次更改任何源文件时,都需要重新启动Flask服务器。