Django pymysql only connects to the database once. How to operate the database?

def go_add (request):

if request.method == "POST":
    
    class_name = request.POST.get("cname")
    
    conn=pymysql.connect(host="192.168.2.130",user="root",password="root",database="test",charset="utf8")
    
    cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)

    sql="insert into user(user) values(%s);"
    -sharpexecutemany
    cursor.execute(sql,class_name)
    -sharp
    conn.commit()
    
    conn.close()
    
    cursor.close()
    
    return redirect("../index/")
    
return render(request, "add.html")
< H1 > this is a routine operation, but each time a new function is written, the database is linked < / H1 >
Jul.06,2021

try this

https://github.com/LuciferJack/python-mysql-pool

what I want is a database connection pool that now links the database independently every time a request is made. If you create a database connection pool, then the whole project will only connect to the database once

.
Menu