Creating Tables using Connector/Python

The code in the following listing creates a database and two tables.

Expected Output:

How it works:

  1. In line 5-10, we define tables using the OrderedDict dictionary instead of a regular dictionary, this is because the regular dictionary doesn’t preserve the order of elements.
  2. In line 11, we try to change the database using the database property of the connection object. If the database doesn’t exist we call create_db() to create the database.
  3. In line 12-20, we create tables by looping over the items in the tables dictionary.

Now, try deleting the post table and run the script again. This time you will get the following output:

Leave a Comment