1

Here is the thing: I'm trying to make a new MySQL database by importing the name for the database and then creating it.

Here is the code:

import mysql.connector as mcon

# DB Connect
stdb = mcon.connect(
    host        = "localhost",
    user        = "root",
    password    = "pass",
    database    = "trffc_int_data")
cursor = stdb.cursor()

# Creating new DB
NewDB = input(" Enter the Database name : ")
sqlsynt = "CREATE DATABASE IF NOT EXISTS %s"
cursor.execute(sqlsynt,NewDB)                 
cursor.commit()
print ("New database created!") 

Looks right, but I keep getting error "1065 >> You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s'<<"

It points the line: cursor.execute(sqlsynt,NewDB)

What could be the problem?

1 Answer 1

1

You can't bind object names (in this case - the database name) like that, only values. You'll have to resort to string formatting. E.g.:

newDB = input(" Enter the Database name : ")
sqlsynt = f"CREATE DATABASE IF NOT EXISTS {newDB}"
cursor.execute(sqlsynt,NewDB)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.