So it's an Access database.
Tough luck. They have no triggers.
Well then. I suppose you know how to insert a row into Access using Python, so I won't go through that.
I'll just build a scenario just after having inserted a row.
CREATE TABLE tableA (
id INTEGER
, name VARCHAR(20)
, dob DATE
, PRIMARY KEY (id)
)
;
INSERT INTO tableA(id,name,dob) VALUES(42,'Arthur Dent','1957-04-22');
INSERT INTO tableA(id,name,dob) VALUES(43,'Ford Prefect','1900-08-01');
INSERT INTO tableA(id,name,dob) VALUES(44,'Tricia McMillan','1959-03-07');
INSERT INTO tableA(id,name,dob) VALUES(45,'Zaphod Beeblebrox','1900-02-01');
CREATE TABLE tableB (
id INTEGER
, name VARCHAR(20)
, dob DATE
, PRIMARY KEY (id)
)
;
INSERT INTO tableB(id,name,dob) VALUES(43,'Ford Prefect','1900-08-01');
INSERT INTO tableB(id,name,dob) VALUES(44,'Tricia McMillan','1959-03-07');
INSERT INTO tableB(id,name,dob) VALUES(45,'Zaphod Beeblebrox','1900-02-01');
Ok. Scenario ready.
Merge ....
MERGE
INTO tableB t
USING tableA s
ON s.id = t.id
WHEN NOT MATCHED THEN INSERT (
id
, name
, dob
) VALUES (
s.id
, s.name
, s.dob
);
42000:1:-3500:[Microsoft][ODBC Microsoft Access Driver]
Invalid SQL statement;
expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'.
So, Merge not supported.
Trying something else:
INSERT INTO tableB
SELECT * FROM tableA
WHERE id <> ALL (SELECT id FROM tableB)
;
1 row inserted
Or:
-- UNDO the previous insert
DELETE FROM tableB WHERE id=42;
1 row deleted
-- retry ...
INSERT INTO tableB
SELECT * FROM tableA
WHERE id NOT IN (SELECT id FROM tableB)
;
1 row inserted
You could run it like here above.
Or, if your insert into Table A, from Python, was:
INSERT INTO tableA(id,name,dob) VALUES(?,?,?);
... and you supplied the values for id, name and dob via host variables,
you could continue with:
INSERT INTO tableB
SELECT * FROM tableA a
WHERE id=?
AND NOT EXISTS(
SELECT * FROM tableB WHERE id=a.id
);
You would still have the value 42 in the first host variable, and could just reuse it. It would be faster this way in case of single row inserts.
Should you perform mass inserts, then, I would insert all new rows to table A, then run the INSERT ... WHERE ... NOT IN
or the INSERT ... WHERE id <> ALL ....
.