from flask import Flask
import pymysql
app = Flask(__name__)
conn = pymysql.connect(host='localhost',
user='xxx',
passwd='yyy',
db='zzz',
charset='utf8')
cur = conn.cursor(pymysql.cursors.DictCursor)
@app.route('/')
def hello_world():
try:
cur.execute("select * from news where id=209")
except Exception as e:
print(e)
print(cur.fetchall())
return "aaa"
if __name__ == '__main__':
app.run()
/etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
127.0.0.1 - - [28/Jun/2014 12:53:01] "GET / HTTP/1.1" 200 -
(2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")
from flask import Flask, g
import pymysql
app = Flask(__name__)
@app.before_request
def before_request():
g.conn = pymysql.connect(host='localhost',
user='pm',
passwd='wccme',
db='pcm',
charset='utf8')
g.cur = g.conn.cursor(pymysql.cursors.DictCursor)
@app.teardown_request
def close_mysql(exception=None):
g.conn.close()
@app.route('/')
def hello_world():
try:
g.cur.execute("select * from news where id=209")
except Exception as e:
print(e)
print(g.cur.fetchall())
return "aaa"
if __name__ == '__main__':
app.run()