Search code examples
pythonmysqlconnection

Is there a way to connect mysql while a python application running?


I'd like to make an python application and retrieve query results from mysql server.

So every time when an event happened, the application connect to mysql through mysqlclient like below

# retrieve query result from db
db=_mysql.connect(host="localhost",user="root",
                  passwd="password",db="db")

Is there a way not to try to connect every time but keep the connection?


Solution

  • Change

    begin loop:
        get MQ msg
        db=_mysql.connect(host="localhost",user="root",
                      passwd="password",db="db")
        db.insert()
    

    to

    db=_mysql.connect(host="localhost",user="root",
                      passwd="password",db="db")
    begin loop:
        get MQ msg
        db.insert()