helper.py
ImportPymysql fromSettingsImportConfigdefConnect (): Conn=Config.POOL.connection () cursor= Conn.cursor (Cursor=pymysql.cursors.dictcursor)#display in a dictionary returnConn,cursordefConnect_close (conn,cursor): Cursor.close () conn.close ()defFetch_all (Sql,args): Conn,cursor=Connect () cursor.execute (sql, args) record_list=Cursor.fetchall () connect_close (conn,cursor)returnrecord_listdeffetch_one (sql, args): conn, cursor=Connect () cursor.execute (SQL, args) result=Cursor.fetchone () connect_close (conn, cursor)returnresultdefinsert (SQL, args): conn, cursor=Connect () row=cursor.execute (sql, args) conn.commit () Connect_close (conn, cursor)returnRow
View Code
settings.py
fromDbutils.pooleddbImportPooleddb, ShareddbconnectionImportPymysql#Download DBUTILS:PIP3 Install dbutilsclassConfig (object): SALT= b"SDF1123DF"Secret_key='ASDF123SDFSDFSDF'Max_content_length= 1024 * 1024 * 7POOL=Pooleddb (creator=pymysql,#modules that use linked databasesMaxconnections=6,#the maximum number of connections allowed for a connection pool, 0 and none means no limit on the number of connectionsmincached=2,#at least 0 of the free links created in the link pool are not created when initializingMaxcached=5,#most idle links in the link pool, 0 and none are not limitedMaxshared=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 errorMaxusage=none,#the maximum number of times a link is reused, none means unlimitedSetsession=[],#a 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. # for example: 0 = None = never, 1 = default = Whenever it is requested, 2 = When a cursor is created, 4 = When a query is executed, 7 = Alwayshost='127.0.0.1', Port=3306, the user='Root', Password='123456', Database='DB2', CharSet='UTF8' )
View Code
Views
@account. Route ('/login/', methods=['GET','POST'])deflogin ():" "Login: return:" " ifRequest.method = ='GET': returnRender_template ('login.html') Username= Request.form.get ('User') Password= Request.form.get ('pwd') Pwd_md5=MD5 (password) data= Helper.fetch_one ("Select Id,nickname from UserInfo where user=%s and pwd =%s", (username, pwd_md5))if notData:returnRender_template ('login.html', error='User name password error') #session[' user_info ') = Datasession['User_info'] = {'ID': data['ID'],'Nickname': data['Nickname']} returnredirect'/home/')
View Code
Connection pooling based on Pymsql database