Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions bookworm/CreateDatabase.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(self,dbname=None):
self.username=config.get("client","user")
self.password=config.get("client","password")
self.conn = None

def connect(self, setengine=True):
#These scripts run as the Bookworm _Administrator_ on this machine; defined by the location of this my.cnf file.
self.conn = MySQLdb.connect(read_default_file="~/.my.cnf",use_unicode='True', charset='utf8', db='', local_infile=1)
Expand All @@ -52,13 +52,27 @@ def connect(self, setengine=True):
"you may need to add \"default-storage-engine=MYISAM\" manually "
"to the [mysqld] user in /etc/my.cnf. Trying again to connect...")
self.connect(setengine=False)


#Allows a user to create another database by firstly creating new instantiation of DB class and calling this createDatabase function, further queries can then be excecuted using the query function below.
def createDatabase(self, dbname=None):
if dbname == None:
print "You did not provide a database name so we are using the one provided previously%s." % (self.dbname)
else:
self.dbname = dbname
#create a connector
self.conn = MySQLdb.connect(read_default_file="~/.my.cnf",use_unicode='True', charset='utf8', db='', local_infile=1)
#Create cursor
cursor = self.conn.cursor()
try:
cursor.execute("CREATE DATABASE IF NOT EXISTS %s" % self.dbname)
#Don't use native query attribute here to avoid infinite loops
cursor.execute("SET NAMES 'utf8'")
cursor.execute("SET CHARACTER SET 'utf8'")
except:
logging.error("Unable to create database")

#Allows user to run queries on new database
def query(self, sql):
"""
Billy defined a separate query method here so that the common case of a connection being
timed out doesn't cause the whole shebang to fall apart: instead, it just reboots
the connection and starts up nicely again.
"""
logging.debug(sql)
try:
cursor = self.conn.cursor()
Expand Down