Skip to content Skip to sidebar Skip to footer

Create Mysql Database In Python Using The %s Operator

Trying to create a database where the name is given through the %s operator. import mysql.connector, MySQLdb db_name='SomeString' #create connection to mysql mydb=mysql.connector.

Solution 1:

I think you are intending the value of db_name to be inserted instead of the %s, like a placeholder in C. This doesn't work as you have found out. Instead, you could do something like:

create_statement = "CREATE DATABASE {:s}".format(db_name)
mycursor.execute(create_statement)

Doing it this way will allow you to use the technique in more complex situations where there is more SQL after the value you are trying to substitute.

Post a Comment for "Create Mysql Database In Python Using The %s Operator"