I'm having difficulty creating a new database programatically through Python.
Given this example:
import MySQLdb as mysql
db = mysql.connect(user="root", host="localhost", passwd="****")
c = db.cursor()
dbName = 'testDB'
c.execute("""create database %s;""", (dbName,))
gives me the following:
Traceback (most recent call last):
File "<input>", line 1, in ?
File "C:\Python24\lib\site-packages\MySQLdb\cursors.py", line 163, in execute
self.errorhandler(self, exc, value)
File "C:\Python24\lib\site-packages\MySQLdb\connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1064, "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 ''testDB'' at line 1")
What I gather from this is the command I'm actually trying to issue here is:
create database 'testDB';
Now this is obviously not going to work. However, how do I use a variable in a create database command without encountering this problem.
I can see in the API, there is actually a
create_db()
method, but MySQLdb doesn't seem to give me access to that method.
I currently use
select_db()
to get around a similar problem I was having with the use command in MySQL.
So does anyone have a way around this?
I'm building an application where I would like users to create and drop databases so I'm going to have a similar problem with dropping the database.
Regards
Patrick