Search code examples
pythonmysqlpython-3.xmysql-connectormysql-connector-python

i cant use python input to create mysql database


I want to take input from user of creating a mysql database I cant use python input to create mysql databasewhat i tryed

Getting this error please help the error


Solution

  • execute() method parameters must be provided as a tuple, dict or a list :

    cursor.execute(cdb, (dbname,))
    

    And I think you can execute your query directly like :

    %-formatting

    cdb = 'CREATE DATABASE %s' % dbname 
    cursor.execute(cdb)
    

    F-strings

    cdb = f'CREATE DATABASE {dbname}'
    cursor.execute(cdb)
    

    str.format()

    cdb = 'CREATE DATABASE {}'.format(dbname) 
    cursor.execute(cdb)
    

    Consider using f-strings when dealing with string that contains variables.

    cdb = f'CREATE DATABASE {dbname}'