O'Reilly logo

MySQL for Python by Albert Lukaszewski, PhD

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Doing it in Python

As you might expect, affecting the creation and deletion of databases and tables in Python is very similar to MySQL when using MySQL for Python. There are some differences as we shall see in this section.

For the following examples, we will work with the following code being assumed:

import MySQLdb
mydb = MySQLdb.connect('localhost', 'skipper', 'secret', 'csv')
cur = mydb.cursor()

Creating databases with MySQLdb

Where Cursor.execute() shows the number of affected rows in previous commands, whether INSERT or SELECT, it always returns a 0 value for the CREATE command of a database:

Testing the output

Consequently, passing the output of the method to a variable will result in that variable equating to 0:

>>> res = cur.execute("CREATE ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required