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
Post a Comment