How to do it...

The entire code of our GUI_MySQL_class.py module is present in the code folder of this chapter, which is available for download from https://github.com/PacktPublishing/Python-GUI-Programming-Cookbook-Second-Edition. It creates the database, adds tables to it, and then inserts data into the two tables we created.

Here, we will outline the code without showing all the implementation details in order to preserve space because it would take too many pages to show the entire code:

import MySQLdb as mysql 
import Ch07_Code.GuiDBConfig as guiConf

class MySQL():
# class variable
GUIDB = 'GuiDB'
#------------------------------------------------------
def connect(self):
# connect by unpacking dictionary credentials
conn = mysql.connector.connect(**guiConf.dbConfig)
# create cursor
cursor = conn.cursor()
return conn, cursor
#------------------------------------------------------
def close(self, cursor, conn):
# close cursor
#------------------------------------------------------
def showDBs(self):
# connect to MySQL
#------------------------------------------------------
def createGuiDB(self):
# connect to MySQL
#------------------------------------------------------
def dropGuiDB(self):
# connect to MySQL
#------------------------------------------------------
def useGuiDB(self, cursor):
'''Expects open connection.'''
# select DB
#------------------------------------------------------
def createTables(self):
# connect to MySQL
# create Table inside DB
#------------------------------------------------------
def dropTables(self):
# connect to MySQL
#------------------------------------------------------
def showTables(self):
# connect to MySQL
#------------------------------------------------------
def insertBooks(self, title, page, bookQuote):
# connect to MySQL
# insert data
#------------------------------------------------------
def insertBooksExample(self):
# connect to MySQL
# insert hard-coded data
#------------------------------------------------------
def showBooks(self):
# connect to MySQL
#------------------------------------------------------
def showColumns(self):
# connect to MySQL
#------------------------------------------------------
def showData(self):
# connect to MySQL
#------------------------------------------------------
if __name__ == '__main__':
# Create class instance
mySQL = MySQL()

Running the preceding code creates the following tables and data in the database we created:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.58.194