Skip to content

Instantly share code, notes, and snippets.

@leonahi
Last active July 17, 2017 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonahi/6053384 to your computer and use it in GitHub Desktop.
Save leonahi/6053384 to your computer and use it in GitHub Desktop.
join_database.py joins two database table created in python using sqlite3 module. For eg. if first database has columns (Id, Data, Time, col1, col2, col3) and second database has columns (Id, Data, Time, col4, col5, col6) then the output of join_database() will be database table containing columns (Id, Data, Time, col1, col2, col3, col4, col5, c…
import sqlite3 as lite
import sys
def join_database(database1Name, database2Name, database3Name):
''' Join two database 'database1Name' and 'database2Name' into database 'database3Name' '''
con1 = lite.connect(database1Name) # Connect to Database1
con2 = lite.connect(database2Name) # Connect to Database2
con3 = lite.connect(database3Name) # Connect to Database3
con1.row_factory = lite.Row # For reading column names from Database1
con2.row_factory = lite.Row # For reading column names from Database2
con1_new = lite.connect(database1Name) # Create new connection with Database1 for reading data
con2_new = lite.connect(database2Name) # Create new connection with Database2 for reading data
with con1, con2, con3, con1_new, con2_new:
cur1 = con1.cursor() # Get cursor from connection 1 (con1)
cur2 = con2.cursor() # Get cursor from connection 2 (con2)
cur3 = con3.cursor() # Get cursor from connection 3 (con2)
cur1_new = con1_new.cursor() # Get connection from connection 1_new (con1_new)
cur2_new = con2_new.cursor() # Get connection from connection 2_new (con2_new)
cur1.execute("SELECT * FROM your_table_name") # Select all column names from table your_table_name in Database1 (Why column names see line no. 11)
cur2.execute("SELECT * FROM your_table_name") # Select all column names from table your_table_name in Database2 (Why column names see line no. 12)
row_1 = cur1.fetchone() # Fetch column names from database1 table
row_2 = cur2.fetchone() # Fetch column names from database2 table
columnNames = row_1.keys() # Read column names from Database1
for item in row_2.keys(): # Append column names from Database1 with Database2, including duplicate column names only once (See description)
if item not in row_1.keys():
columnNames.append(item)
cur3.execute("CREATE TABLE IF NOT EXISTS your_table_name(Id INTEGER PRIMARY KEY, Date TEXT(10), Time TEXT(8))") # You can change this line according to your needs
for col in columnNames[3:]:
cur3.execute("ALTER TABLE your_table_name ADD {} DOUBLE".format(col)) # You can change the data type(here it is DOUBLE) according to your needs
cur1_new.execute("SELECT * FROM your_table_name") # Select complete data at once from table your_table_name in database1
cur2_new.execute("SELECT * FROM your_table_name") # Select complete data at once from table your_table_name in database2
rows1 = cur1_new.fetchall() # Read all rows from Database1
rows2 = cur2_new.fetchall() # Read all rows from Database2
rows1 = [r[1:] for r in rows1] # Except first row in rows1 read all rows
rows2 = [r[3:] for r in rows2] # Except first three rows in rows2 read all rows
rows = [r1 + r2 for r1, r2 in zip(rows1, rows2)] #Combine each rows in rows1 and rows2 element wise
ques = [] # Use by sqlite for inserting into table
ques = ["?"]*len(columnNames[1:]) # Generate list [?, ?, ?, ?,........till length equals length of columnNames[1:]
ques = ",".join(ques) # Generate string "?,?,?,?,?........"
columnNames = ",".join(columnNames[1:]) # Generate string "col1, col2, col3............"
for item in rows: # Insert combined data into new Database3
cur3.execute("INSERT INTO your_table_name({0}) VALUES ({1})".format(columnNames, ques), item)
if __name__ == '__main__':
pass
@GopikaGopinath
Copy link

what if the database is not created using the sqlite3 module?.Can you provide the code for the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment