I know we can pass variable in SQL in python like this:
cur.execute("INSERT INTO foo VALUES (%s)", ["bar"])
But I'm trying to create a database after user input and facing error. This is the code which is not working:
import mysql.connector;
host = input("ENter Host name ");
user = input("ENter Username ");
password = input("ENter password ");
conn = mysql.connector.connect(
host = host,
user = user,
password = password
);
connect = conn.cursor();
print("Connection Setup");
db = input("Enter Database Name ");
connect.execute('CREATE DATABASE %s',(db,));
print("Database ",db," created successfully");
connect.execute("SHOW DATABASES");
for x in connect:
print(x);
But I'm not able to create the database... What am I doing wrong?
Never mind I found the answer. Here it is
connect.execute("""CREATE DATABASE {}""".format(db));
Just the wrong formatting on my end. you can use this as well
connect.execute("CREATE DATABASE {}".format(db));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.