Python MySQLdb substituting strings into mysql conn -


i have connection line below , want substitute in variables, cannot work:

db = mysqldb.connect(host="localhost", user="root", passwd="password", db="database") 

i change use variable:

db = mysqldb.connect(host="localhost", user="%s", passwd="password", db="database") % "root" 

error: mysql_exceptions.operationalerror: (1045, "access denied user '%s'@'localhost' (using password: yes)")

so, not substituting , tries run query %s. tried .format (instead of %s) , got same issue.

to around this, going substitute line beforehand , add in connection line 1 argument, basic version of doesn't work me. here trial run, fails without substituation:

variable = '''mysqldb.connect(host="localhost", user="root", passwd="password", db="database")'''  db = mysqldb.connect(variable) 

this results in mysql trying pass entire variable first argument (the host): _mysql_exceptions.operationalerror: (2005, 'unknown mysql server host \'host="localhost", user="root", passwd="password", db="database"\' (2)')

i think you're on thinking this. standard function call, , has nothing @ specific mysqldb. call, if want use variable, so:

user = 'root' mysqldb.connect(host="localhost", user=user, passwd="password", db="database") 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -