Sql to merge python tuples into database on keys?

Viewed 85

I have a SQL database and a list of python tuples with values ordered by column.

I simply want to insert the tuples into a SQL database, merged on some key columns, say, 'a', 'b', and 'c'.

I have a database connection but I don't know how to efficiently set up a SQL statement that does this.

It MUST be efficient, I have like 5 million rows.

2 Answers

you may need something like this:

tuples_list = [
    (114, 3452, 3343),
    (414, 5213, 634),
    (347, 8534, 933)
]

for a, b, c in tuples_list:
    sql_text = 'insert into table_name (column_a , column_b , column_c) values ( {a_value}, {b_value}, {c_value} )'.format(a_value=a, b_value=b, c_value=c)
    print(sql_text)

    # command_to_execute(sql_text) 

Use executemany, with a parametrized query.

dataToInsert = [
    (10, 'Parent 10'),
    (20, 'Parent 20'),
    (30, 'Parent 30'),
    (40, 'Parent 40'),
    (50, 'Parent 50')
]
cursor.executemany("insert into ParentTable (a,b) values (:1, :2)", dataToInsert)
Related