passing vars into mysql.connector.connect

1.7k views Asked by At

In python I am trying to pass variables into the connect method like below,

cnx = mysql.connector.connect(user="%s", password="%s",host="%s",database="%s") % (dbuser,dbpassword,dbhost,dbdatabase) (port is missing)

What would be the best way to pass values into this kind of method ?

2

There are 2 answers

0
alecxe On BEST ANSWER

You can create a dictionary and deliver keyword arguments by unpacking it:

params = {
    'user': dbuser, 
    'password': dbpassword,
    'host': dbhost,
    'database': dbdatabase
}

cnx = mysql.connector.connect(**params)
0
BrenBarn On

You can just pass them directly:

cnx = mysql.connector.connect(user=dbuser, password=dbpassword, host=dbhost, database=dbdatabase)