Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk Insert without Context Manager #91

Open
hub-il opened this issue Jun 29, 2021 · 1 comment
Open

Bulk Insert without Context Manager #91

hub-il opened this issue Jun 29, 2021 · 1 comment

Comments

@hub-il
Copy link

hub-il commented Jun 29, 2021

The bulk_insert command does not work if it is used without a context manager, returns a value for the number of rows that were supposed to be inserted and does not throw an error:

Works:

    with sql_cnxn as connection:
        return_result = connection.bulk_insert(table_name, bulk_insert_data)

Does not work:

return_result = sql_cnxn.bulk_insert(table_name, bulk_insert_data)
@joshuahlang
Copy link
Collaborator

Could you provide code to reproduce this error?

The following code works as expected without the context manager:

import pprint
import ctds

print('ctds version: ' + '.'.join(map(str, ctds.version_info)))
print('FreeTDS version: ' + ctds.freetds_version)

connection = ctds.connect(
    'localhost',
    user='SA',
    password='cTDS-unitest123'
)
with connection.cursor() as cursor:
    cursor.execute('SELECT @@VERSION')
    print(cursor.fetchone()[0])

    cursor.execute(
        '''
        DROP TABLE IF EXISTS RoundingTest
        '''
    )
    cursor.execute(
        '''
        CREATE TABLE RoundingTest(
            NumericColumn NUMERIC(8,2),
            NumericColumn2 NUMERIC(12,5)
        )
        '''
    )

connection.bulk_insert(
    'RoundingTest',
    [
        [77.4930] * 2,
        [339.6490] * 2,
    ]
)

with connection.cursor() as cursor:
    cursor.execute(
        '''
        SELECT * FROM RoundingTest
        '''
    )

    pprint.pprint(
        list(map(tuple, cursor.fetchall()))
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants