helper.py
1 Import Pymysql2 3 fromSettings Import Config4 5 def Connect ():6conn =Config.POOL.connection ()7cursor = Conn.cursor (cursor=pymysql.cursors.DictCursor) # Display in a dictionary8 returnConn,cursor9 Ten One def connect_close (conn,cursor): A cursor.close () - conn.close () - the def fetch_all (Sql,args): -Conn,cursor =Connect () - - cursor.execute (sql, args) +Record_list =Cursor.fetchall () - connect_close (conn,cursor) + A returnrecord_list at - - def fetch_one (sql, args): -conn, cursor =Connect () - cursor.execute (sql, args) -result =Cursor.fetchone () in Connect_close (conn, cursor) - to returnresult + - the def insert (sql, args): *conn, cursor =Connect () $row =cursor.execute (sql, args)Panax Notoginseng Conn.commit () - Connect_close (conn, cursor) the returnRow
View Code
settings.py
1 fromdbutils.pooleddb import pooleddb, shareddbconnection2 Import Pymysql3 # download DBUTILS:PIP3 install dbutils4 classConfig (Object):5SALT = b"SDF1123DF"6Secret_key ='ASDF123SDFSDFSDF'7Max_content_length =1024x768*1024x768*78 9 TenPOOL =Pooleddb ( OneCreator=Pymysql, # Using a linked database module Amaxconnections=6, # The maximum number of connections allowed for a connection pool, 0 and none means no limit on the number of connections -Mincached=2at least 0 of the free links created in the link pool are not created when the # is initialized -Maxcached=5, # Link pool up to idle links, 0 and none unlimited theMaxshared=3, - # The maximum number of links shared in a linked pool, 0 and none means sharing all. PS: Useless, because Pymysql and mysqldb modules such as threadsafety are 1, all values regardless of set to how much, _maxcached forever is 0, so forever is all links are shared. -blocking=True, # If there are no available connections in the connection pool, wait is blocked. True, wait, False, not wait and then error -Maxusage=None, # The maximum number of times a link is reused, none means unlimited +Setsession=[], # The list of commands to execute before starting the session. such as: ["set Datestyle to ...","Set time zone ..."] -ping=0, +# Ping the MySQL server to check if the service is available. such as0= None = Never,1=default= Whenever it isRequested,2= When a cursor isCreated4= When a query isExecuted7= always Ahost='127.0.0.1', atport=3306, -User='Root', -password='123456', -Database='DB2', -charset='UTF8' -)
View Code
views.py
1@account. Route ('/login/', methods=['GET','POST'])2 def login ():3 " "4 Login5:return:6 " "7 ifRequest.method = ='GET':8 returnRender_template ('login.html')9 TenUsername = Request.Form.Get('User') OnePassword = Request.Form.Get('pwd') A -PWD_MD5 =MD5 (password) - thedata = Helper.fetch_one ("Select Id,nickname from UserInfo where user=%s and pwd =%s", (username, pwd_md5)) - - ifNot data: - returnRender_template ('login.html', error='User name password error') + -# session['User_info'] =Data +session['User_info'] = {'ID': data['ID'],'Nickname': data['Nickname']} A at returnredirect'/home/')
View Code
Flask-Using MySQL data pool