Insert unmatched records to another database connection

Viewed 39

I have 2 database connections. I want to compare a single table from each connection to each other. And if there are unmatched records, I want to add them to the table database where they are missing.

This is what I came up with but id doesn't seem to do the inserting part. I'm new to python excuse the code thanks.

# establishing connections and querying the database

import sqlite3
con1 = sqlite3.connect("database1.db")
cur1 = con1.cursor()
table1 = cur1.execute("SELECT * FROM table1")
fetch_table1 = table1.fetchall()
mylist = list(table1)


con2 = sqlite3.connect("database2.db")
cur2 = con2.cursor()
table2= cur2.execute("SELECT * FROM table2")
table2 = table2.fetchall()
mylist2 = list(table2). 

# finding unmatched eliminates and inserting them to the database

def non_match_elements(mylist2, mylist):
    non_match = []
    for i in mylist2:
        if i not in mylist:
            non_match.append(i)
            non_match = non_match_elements(mylist2, mylist)
        cur1.executemany("""INSERT INTO table 1 VALUES (?,?,?)""", non_match)

con1.commit()
res = cur1.execute("select column from table1")
print(res.fetchall())

Thanks again guys

1 Answers

I would suggest ATTACHing one connection to the other, you then the have two INSERT INTO table SELECT * FROM table2 WHERE query that would insert from one table to the other.

here's an example/demo (not of the ATTACH DATABASE but of aligning two tables with the same schema but with different data):-

/* Cleanup - just in case*/
DROP TABLE IF EXISTS table1;
DROP TABLE IF EXISTS table2;
/* Create the two tables */
CREATE TABLE IF NOT EXISTS table1 (val1 TEXT, val2 TEXT, val3 TEXT);
CREATE TABLE IF NOT EXISTS table2 (val1 TEXT, val2 TEXT, val3 TEXT);
/*****************************************************************/
/* load the two different sets of data and also some common data */
INSERT INTO table1 VALUES ('A','AA','AAA'),('B','BB','BBB'),('C','CC','CCC'),('M','MM','MMM');
INSERT INTO table2 VALUES ('X','XX','XXX'),('Y','YY','YYY'),('Z','ZZ','ZZZ'),('M','MM','MMM');
/*************************************************************/
/* Macth each table to the other using an INSERT .... SELECT */
/*************************************************************/
INSERT INTO table1 SELECT * FROM table2 WHERE val1||val2||val3 NOT IN (SELECT(val1||val2||val3) FROM table1);
INSERT INTO table2 SELECT * FROM table1 WHERE val1||val2||val3 NOT IN (SELECT(val1||val2||val3) FROM table2);
/* Output both tables */
SELECT 'T1',* FROM table1;
SELECT 'T2',* FROM table1;
/* Cleanup */
DROP TABLE IF EXISTS table1;
DROP TABLE IF EXISTS table2;

The results of the 2 SELECTS being :-

enter image description here

and

enter image description here

  • the first column (T1 or T2) just being used to indicate which table the SELECT is from.
  • table1 has the X,Y and Z values rows copied from table2
  • table2 has the A,B and C values rows copied from table1
  • the M values row, as they exist in both remain intact, they are neither duplicated nor deleted.
  • Thus data wise the two tables are identical.
Related