简体   繁体   中英

insert unmatched records to another database connection

so 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 add them to the table database where its missing.

this is what i came up with but id doesn't seem to do the inserting part. im new to python excuse the code thanks.

#establishing connections and quarrying 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

I would suggest ATTACH ing 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:-

在此处输入图像描述

and

在此处输入图像描述

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM