Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

basic pyodbc bulk insert

Tags:

In a python script, I need to run a query on one datasource and insert each row from that query into a table on a different datasource. I'd normally do this with a single insert/select statement with a tsql linked server join but I don't have a linked server connection to this particular datasource.

I'm having trouble finding a simple pyodbc example of this. Here's how I'd do it but I'm guessing executing an insert statement inside a loop is pretty slow.

result = ds1Cursor.execute(selectSql)

for row in result:
    insertSql = "insert into TableName (Col1, Col2, Col3) values (?, ?, ?)"
    ds2Cursor.execute(insertSql, row[0], row[1], row[2])
    ds2Cursor.commit()

Is there a better bulk way to insert records with pyodbc? Or is this a relatively efficient way to do this anyways. I'm using SqlServer 2012, and the latest pyodbc and python versions.

like image 804
Zip184 Avatar asked May 03 '16 15:05

Zip184


People also ask

How do I make inserts into SQL Server 100x faster with Pyodbc?

Usually, to speed up the inserts with pyodbc , I tend to use the feature cursor. fast_executemany = True which significantly speeds up the inserts.

Can Pyodbc execute multiple queries?

The API in the pyodbc connector (or pymysql) doesn't allow multiple statements in a SQL call.

What is bulk insert?

A Bulk insert is a process or method provided by a database management system to load multiple rows of data into a database table. Bulk insert may refer to: Transact-SQL BULK INSERT statement. PL/SQL BULK COLLECT and FORALL statements. MySQL LOAD DATA INFILE statement.


1 Answers

Here's a function that can do the bulk insert into SQL Server database.

import pyodbc
import contextlib

def bulk_insert(table_name, file_path):
    string = "BULK INSERT {} FROM '{}' (WITH FORMAT = 'CSV');"
    with contextlib.closing(pyodbc.connect("MYCONN")) as conn:
        with contextlib.closing(conn.cursor()) as cursor:
            cursor.execute(string.format(table_name, file_path))
        conn.commit()

This definitely works.

UPDATE: I've noticed at the comments, as well as coding regularly, that pyodbc is better supported than pypyodbc.

NEW UPDATE: remove conn.close() since the with statement handles that automatically.

like image 57
Naufal Avatar answered Sep 20 '22 17:09

Naufal