Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQLdb python 2.7_Python_Mysql_Python 2.7_Mysql Python - Fatal编程技术网

MySQLdb python 2.7

MySQLdb python 2.7,python,mysql,python-2.7,mysql-python,Python,Mysql,Python 2.7,Mysql Python,主要目的是将变量传递到语句中,但它不适合mysql语法。我可以得到一些关于我在这里做错了什么的指导吗 def sql_usercreation(endpoint, dbuser, dbpassword, username, password): db = MySQLdb.connect(endpoint, dbuser, dbpassword) cursor = db.cursor() cstmnt = cursor.execute("CREATE USER IF N

主要目的是将变量传递到语句中,但它不适合mysql语法。我可以得到一些关于我在这里做错了什么的指导吗

def sql_usercreation(endpoint, dbuser, dbpassword, username, password):

    db = MySQLdb.connect(endpoint, dbuser, dbpassword)

    cursor = db.cursor()
    cstmnt = cursor.execute("CREATE USER IF NOT EXIST %s@'%' IDENTIFIED BY %s",[username,password])
    dbguser = cursor.execute("select user,host from mysql.user")
    dboutput = cursor.fetchall()
    db.commit()
    db.close()
    print(dboutput)

sql_usercreation("hostendpointaddress", "dbadmin", "passworddbadmin", "username", "password")
错误:

    query = query % args
ValueError: unsupported format character ''' (0x27) at index 30

您必须将
%%
字符转义为
%%

cursor.execute("CREATE USER IF NOT EXIST %s@'%%' IDENTIFIED BY %s",[username,password])