[Tutor] Create new MySQL database from user input.
Michael Janssen
Janssen@rz.uni-frankfurt.de
Mon Jun 16 04:40:03 2003
On Sun, 15 Jun 2003, Decibels wrote:
> Sorry to keep bring MySQL issues up guys, but I will get quite far
> than hit a stumbling block.
Hello ,
try this:
>>> DBName = "db"
>>> print "CREATE DATABASE %s" (DBName)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'str' object is not callable
Nothing of MySQLdb just string-format convention: you've forgotten the %
sign (Python just see the brackets and tries to call "CREATE DATABASE %s"
like a function):
>>> print "CREATE DATABASE %s" % (DBName)
CREATE DATABASE db
Try, if this solves all the mess.
Michael
>
> I am at the point of allowing the user to put in the option to Create a
> different name for the Database. Use input with command-line option
> seems to be working fine. But I have tried quite a few ideas and cannot
> get the syntax correct it appears.
>
> Here are a few of my more 'closer' attempts. Is it syntax or am I way off
> bat.
>
> Actually I have the command line option working, just put this in to
> facilitate things. And see if it was actually working. Results same.
>
> 1)
> DBName = "test" # also tried ('test'), ("test")
> cursor.execute ("CREATE DATABASE %s" (DBName))
>
> Results:
>
> DB doesn't exist. Going to Create it.
> Traceback (most recent call last):
> File "stockpicker6.py", line 101, in ?
> cursor.execute ("CREATE DATABASE %s" (DBName))
> TypeError: 'str' object is not callable
>
> 2)
> DBName = "test"
> cursor.execute ("""CREATE DATABASE SET db = %s
> WHERE db = %s
> """, (None, DBName))
>
>
> Results:
>
> DB doesn't exist. Going to Create it.
> Traceback (most recent call last):
> File "stockpicker6.py", line 101, in ?
> cursor.execute ("""CREATE DATABASE SET db = %s
> File "/usr/lib/python2.2/site-packages/MySQLdb/cursors.py", line 95, in execute
> return self._execute(query, args)
> File "/usr/lib/python2.2/site-packages/MySQLdb/cursors.py", line 114, in _execute
> self.errorhandler(self, exc, value)
> File "/usr/lib/python2.2/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
> raise errorclass, errorvalue
> _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax near 'SET db = NULL \n\t\t WHERE db = 'test'\n\t\t' at line 1")
>
> 3)
> DBName = "test"
> cursor.execute ("""CREATE DATABASE (%s)
> """, (DBName))
>
> Results:
> DB doesn't exist. Going to Create it.
> Traceback (most recent call last):
> File "stockpicker6.py", line 101, in ?
> cursor.execute ("""CREATE DATABASE (%s)
> File "/usr/lib/python2.2/site-packages/MySQLdb/cursors.py", line 95, in execute
> return self._execute(query, args)
> File "/usr/lib/python2.2/site-packages/MySQLdb/cursors.py", line 114, in _execute
> self.errorhandler(self, exc, value)
> File "/usr/lib/python2.2/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
> raise errorclass, errorvalue
> _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax near '('tests') \n\t\t' at line 1")
>
>
> Any ideas? Thanks Dave
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>