Merge pull request #4 from whattheserver/patch-1

pep fixes
master
gopherchucks 4 years ago committed by GitHub
commit 02240eb638
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,37 +17,40 @@ from time import gmtime, strftime
############################ Global Variables ################################## ############################ Global Variables ##################################
################################################################################ ################################################################################
dbCount = 0 # Variable to count the number of databases dbCount = 0 # Variable to count the number of databases
listDB = [] # Variable to store the names of the databases listDB = [] # Variable to store the names of the databases
listTable = [] # Variable to store table names listTable = [] # Variable to store table names
############################ Function Definitions ############################## ############################ Function Definitions ##############################
################################################################################ ################################################################################
# Attaches a database to the currently connected database # Attaches a database to the currently connected database
# #
# @param dbName the name of the database file (i.e. "example.db") # @param db_name the name of the database file (i.e. "example.db")
# @return none # @return none
def attachDatabase( dbName ): def attach_database(db_name):
global dbCount global dbCount
global listDB global listDB
print(("Attaching database: %s") % dbName) print("Attaching database: %s" % db_name)
curs.execute("ATTACH DATABASE ? as ? ;", (dbName, 'db' + str(dbCount))) curs.execute("ATTACH DATABASE ? as ? ;", (db_name, 'db' + str(dbCount)))
listDB.append('db' + str(dbCount)) listDB.append('db' + str(dbCount))
dbCount += 1 dbCount += 1
# Closes the current database connection # Closes the current database connection
# #
# @return none # @return none
def closeConnection(): def close_connection():
curs.close() curs.close()
conn.close() conn.close()
# Gets the table names of a database # Gets the table names of a database
# #
# @param dbName the name of the database file (i.e. "example.db") # @param db_name the name of the database file (i.e. "example.db")
# @return a string array of the table names # @return a string array of the table names
def getTableNames(): def get_table_names():
curs.execute("SELECT name FROM sqlite_master WHERE type='table';") curs.execute("SELECT name FROM sqlite_master WHERE type='table';")
temp = curs.fetchall() temp = curs.fetchall()
tables = [] tables = []
@ -55,27 +58,29 @@ def getTableNames():
tables.append(temp[i][0]) tables.append(temp[i][0])
return tables return tables
# Gets the column names of a table # Gets the column names of a table
# #
# @param dbName the name of the database file (i.e. "example.db") # @param db_name the name of the database file (i.e. "example.db")
# @return a string array of the column names - strips primary ids column # @return a string array of the column names - strips primary ids column
def getColumnNames( tableName ): def get_column_names(table_name):
curs.execute("PRAGMA table_info(%s);" % str(tableName)) curs.execute("PRAGMA table_info(%s);" % str(table_name))
temp = curs.fetchall() temp = curs.fetchall()
columns = [] columns = []
for i in range(0, len(temp)): for i in range(0, len(temp)):
if (("id" in temp[i][1]) | ("ID" in temp[i][1])): if ("id" in temp[i][1]) | ("ID" in temp[i][1]):
continue continue
else: else:
columns.append(temp[i][1]) columns.append(temp[i][1])
return columns return columns
# Compares two lists to see if they have identical data # Compares two lists to see if they have identical data
# #
# @param list1 the first list parameter for comparison # @param list1 the first list parameter for comparison
# @param list2 the second list parameter for comparison # @param list2 the second list parameter for comparison
# @return will return a boolean (0 lists !=, 1 lists ==) # @return will return a boolean (0 lists !=, 1 lists ==)
def compareLists( list1, list2 ): def compare_lists(list1, list2):
if len(list1) != len(list2): if len(list1) != len(list2):
return 0 return 0
else: else:
@ -84,32 +89,34 @@ def compareLists( list1, list2 ):
return 0 return 0
return 1 return 1
# Converts a list of string objects to a string of comma separated items. # Converts a list of string objects to a string of comma separated items.
# #
# @param listObj the list to convert # @param listObj the list to convert
# @return a string containing the list items - separated # @return a string containing the list items - separated
# by commas. # by commas.
def listToString( listObj ): def list_to_string(list_obj):
listString = "" list_string = ""
for i in range(0, len(listObj)): for i in range(0, len(list_obj)):
if (i == (len(listObj) - 1)): if i == (len(list_obj) - 1):
listString = listString + listObj[i] list_string = list_string + list_obj[i]
else: else:
listString = listString + listObj[i] + ", " list_string = list_string + list_obj[i] + ", "
return listString return list_string
# Merges a table from an attached database to the source table # Merges a table from an attached database to the source table
# #
# @param tableName the name of the table to merge # @param table_name the name of the table to merge
# @param columnNames the names of the columns to include in the merge # @param column_names the names of the columns to include in the merge
# @param dbNameTableName the name of the attached database and the table # @param db_name_table_name the name of the attached database and the table
# i.e. "databaseName.tableName" # i.e. "db_name.table_name"
# @return none # @return none
def mergeTable( tableName, columnNames, dbName ): def merge_table(table_name, column_names, db_name):
dbNameTableName = dbName + "." + tableName db_name_table_name = db_name + "." + table_name
try: try:
curs.execute("INSERT INTO %s (%s) SELECT %s FROM %s;" % curs.execute("INSERT INTO %s (%s) SELECT %s FROM %s;" %
(tableName, columnNames, columnNames, dbNameTableName)) (table_name, column_names, column_names, db_name_table_name))
conn.commit() conn.commit()
except: except:
pass pass
@ -119,12 +126,12 @@ def mergeTable( tableName, columnNames, dbName ):
################################################################################ ################################################################################
mainDB = '' # This is where the main database is mainDB = '' # This is where the main database is
# referenced. Where all items will be # referenced. Where all items will be
# merged to. # merged to.
otherDBs = [] otherDBs = []
if (len(otherDBs) == 0): if len(otherDBs) == 0:
print("ERROR: No databases have been added for merging.") print("ERROR: No databases have been added for merging.")
sys.exit() sys.exit()
@ -132,10 +139,10 @@ if (len(otherDBs) == 0):
################################################################################ ################################################################################
# Initialize Connection and get main list of tables # Initialize Connection and get main list of tables
conn = sqlite3.connect(mainDB) # Connect to the main database conn = sqlite3.connect(mainDB) # Connect to the main database
curs = conn.cursor() # Connect a cursor curs = conn.cursor() # Connect a cursor
listTable = getTableNames() # Get the table names listTable = get_table_names() # Get the table names
closeConnection() close_connection()
# Compare databases # Compare databases
startTime = time.time() startTime = time.time()
@ -144,26 +151,26 @@ print("Comparing databases. Started at: " + strftime("%H:%M", gmtime()))
for i in range(0, len(otherDBs)): for i in range(0, len(otherDBs)):
conn = sqlite3.connect(otherDBs[i]) conn = sqlite3.connect(otherDBs[i])
curs = conn.cursor() curs = conn.cursor()
temp = getTableNames() # Get the current list of tables temp = get_table_names() # Get the current list of tables
if (len(listTable) > len(temp)): if len(listTable) > len(temp):
print("Table is missing from non-primary database: %s" % otherDBs[i]) print("Table is missing from non-primary database: %s" % otherDBs[i])
print("Database will NOT BE MERGED with the main database.") print("Database will NOT BE MERGED with the main database.")
otherDBs.remove(otherDBs[i]) # Remove the table to avoid errors otherDBs.remove(otherDBs[i]) # Remove the table to avoid errors
continue continue
if (len(listTable) < len(temp)): if len(listTable) < len(temp):
print("Extra table(s) in non-primary database: %s" % otherDBs[i]) print("Extra table(s) in non-primary database: %s" % otherDBs[i])
print("TABLES that are NOT in main database will NOT be added.") print("TABLES that are NOT in main database will NOT be added.")
if (listTable != temp): if listTable != temp:
print("Tables do not match in non-primary database: %s" % otherDBs[i]) print("Tables do not match in non-primary database: %s" % otherDBs[i])
print("The database will NOT BE MERGED with the main database.") print("The database will NOT BE MERGED with the main database.")
otherDBs.remove(otherDBs[i]) # Remove the table to avoid errors otherDBs.remove(otherDBs[i]) # Remove the table to avoid errors
continue continue
closeConnection() close_connection()
if (len(otherDBs) == 0): if len(otherDBs) == 0:
print("ERROR: No databases to merge. Databases were either removed due to \ print("ERROR: No databases to merge. Databases were either removed due to \
inconsistencies, or databases were not added properly.") inconsistencies, or databases were not added properly.")
sys.exit() sys.exit()
@ -175,19 +182,19 @@ print("Finished comparing databases. Time elapsed: %.3f" % (time.time() -
startTime = time.time() startTime = time.time()
print("Merging databases. Started at: " + strftime("%H:%M", gmtime())) print("Merging databases. Started at: " + strftime("%H:%M", gmtime()))
conn = sqlite3.connect(mainDB) # Attach main database conn = sqlite3.connect(mainDB) # Attach main database
curs = conn.cursor() # Attach cursor curs = conn.cursor() # Attach cursor
for i in range(0, len(otherDBs)): for i in range(0, len(otherDBs)):
attachDatabase(otherDBs[i]) # Attach other databases attach_database(otherDBs[i]) # Attach other databases
# Merge databases # Merge databases
for i in range(0, len(listDB)): for i in range(0, len(listDB)):
for j in range(0, len(listTable)): for j in range(0, len(listTable)):
columns = listToString(getColumnNames(listTable[j])) # get columns columns = list_to_string(get_column_names(listTable[j])) # get columns
mergeTable(listTable[j], columns, listDB[i]) merge_table(listTable[j], columns, listDB[i])
conn.commit() # Commit changes conn.commit() # Commit changes
closeConnection() # Close connection close_connection() # Close connection
print("Databases finished merging. Time elapsed: %.3f" % (time.time() - print("Databases finished merging. Time elapsed: %.3f" % (time.time() -
startTime)) startTime))

Loading…
Cancel
Save