From 5c627569d754f9b048d948544b5c5155696927e0 Mon Sep 17 00:00:00 2001 From: tpmccallum Date: Wed, 27 May 2015 06:05:12 +0000 Subject: [PATCH 1/4] adding function to create new database --- bookworm/CreateDatabase.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/bookworm/CreateDatabase.py b/bookworm/CreateDatabase.py index 549d009..4b61c4b 100755 --- a/bookworm/CreateDatabase.py +++ b/bookworm/CreateDatabase.py @@ -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) @@ -52,13 +52,30 @@ 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 (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): + 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'") + if setengine: + cursor.execute("SET default_storage_engine=MYISAM") + cursor.execute("USE %s" % self.dbname) + except: + logging.error("Forcing default engine failed. On some versions of Mysql, " + "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 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() From 083626854bccd4ea74eca08fe6841eb005c15612 Mon Sep 17 00:00:00 2001 From: Timothy McCallum Date: Thu, 28 May 2015 15:45:04 +1000 Subject: [PATCH 2/4] Update CreateDatabase.py --- bookworm/CreateDatabase.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/bookworm/CreateDatabase.py b/bookworm/CreateDatabase.py index 4b61c4b..80e5522 100755 --- a/bookworm/CreateDatabase.py +++ b/bookworm/CreateDatabase.py @@ -65,9 +65,6 @@ def createDatabase(self, dbname): #Don't use native query attribute here to avoid infinite loops cursor.execute("SET NAMES 'utf8'") cursor.execute("SET CHARACTER SET 'utf8'") - if setengine: - cursor.execute("SET default_storage_engine=MYISAM") - cursor.execute("USE %s" % self.dbname) except: logging.error("Forcing default engine failed. On some versions of Mysql, " "you may need to add \"default-storage-engine=MYISAM\" manually " From 9fe5e58eabba30834b8bc70b9663bf269747b619 Mon Sep 17 00:00:00 2001 From: Timothy McCallum Date: Thu, 28 May 2015 15:49:20 +1000 Subject: [PATCH 3/4] Update CreateDatabase.py --- bookworm/CreateDatabase.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bookworm/CreateDatabase.py b/bookworm/CreateDatabase.py index 80e5522..c615d96 100755 --- a/bookworm/CreateDatabase.py +++ b/bookworm/CreateDatabase.py @@ -54,8 +54,11 @@ def connect(self, setengine=True): self.connect(setengine=False) #Allows a user to create another database (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): - self.dbname = dbname + 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 @@ -66,10 +69,7 @@ def createDatabase(self, dbname): cursor.execute("SET NAMES 'utf8'") cursor.execute("SET CHARACTER SET 'utf8'") except: - logging.error("Forcing default engine failed. On some versions of Mysql, " - "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) + logging.error("Unable to create database") #Allows user to run queries on new database def query(self, sql): From 898886a7236fcb2a6bcfe42c98c04881b248cf2e Mon Sep 17 00:00:00 2001 From: Timothy McCallum Date: Thu, 28 May 2015 15:50:30 +1000 Subject: [PATCH 4/4] Update CreateDatabase.py --- bookworm/CreateDatabase.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookworm/CreateDatabase.py b/bookworm/CreateDatabase.py index c615d96..07460b6 100755 --- a/bookworm/CreateDatabase.py +++ b/bookworm/CreateDatabase.py @@ -53,7 +53,7 @@ def connect(self, setengine=True): "to the [mysqld] user in /etc/my.cnf. Trying again to connect...") self.connect(setengine=False) - #Allows a user to create another database (creating new instantiation of DB class and calling this createDatabase function), further queries can then be excecuted using the query function below. + #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)