2017-05-19 88 views
1

主要目標是將變量傳遞給語句,但它不適合用mysql語法,我可以在這裏得到一些我在做錯的指導嗎?MySQLdb python 2.7

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 %[email protected]'%' 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 

回答

1

你要逃脫你%字符作爲%%

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