def setUp(self): try: # create a test database. self.testName = datetime.datetime.now().strftime( "%Y_%m_%d_%H_%M_%S") MYSQL_URL = "mysql://*****:*****@localhost/test" mysqlUrl = urlparse.urlparse(MYSQL_URL) url = mysqlUrl.hostname password = mysqlUrl.password userName = mysqlUrl.username dbName = mysqlUrl.path[1:] # slice off the '/' db = MySQLdb.connect(host=url, user=userName, passwd=password, db=dbName) cur = db.cursor() cur.execute('create database %s' % self.testName) db.commit() #cur.execute("GRANT ALL ON %s.* TO 'dev'@'localhost';"%self.testName) #db.commit() fibDataDB = FibDataDB(url, self.testName, userName, password) fibDataDB.createTable() db.commit() db.close() except MySQLdb.Error, e: print("error creating table fibdata") print(e)
def initializeDB(url): log.debug("setting up db connection...") urlHost = url.hostname password = url.password user = url.username dbname = url.path[1:] fibDataDB = FibDataDB(urlHost, dbname, user, password) return fibDataDB
def initializeFibDataDB(self, dbName): MYSQL_URL = "mysql://*****:*****@localhost/%s" % dbName mysql_url = urlparse.urlparse(MYSQL_URL) url = mysql_url.hostname password = mysql_url.password user = mysql_url.username dbname = mysql_url.path[1:] fibDB = FibDataDB(url, dbname, user, password) return fibDB
if __name__ == '__main__': try: logging.basicConfig() log = logging.getLogger('fibonacci') log.setLevel(logging.DEBUG) try: mysqlUrl = urlparse.urlparse(os.environ['MYSQL_URL']) except KeyError: log.warn( "env variable MYSQL_URL not found, reverting to DATABASE_URL") mysqlUrl = urlparse.urlparse(os.environ['DATABASE_URL']) url = mysqlUrl.hostname log.debug("url = %s" % url) password = mysqlUrl.password log.debug("password = %s" % password) userName = mysqlUrl.username log.debug("username = %s" % userName) dbName = mysqlUrl.path[1:] # slice off the '/' log.debug("dbName = %s" % dbName) fibdataDB = FibDataDB(url, dbName, userName, password) fibdataDB.createTable() log.info("fibdata table created") except MySQLdb.Error, e: print "Exception during database initialization: %s" % str(e)