damionw / pythonbcp

BCP for SQLServer from Python
Apache License 2.0
5 stars 3 forks source link

Malformed data being sent to the database #2

Open gchilczuk opened 4 years ago

gchilczuk commented 4 years ago

I'm not sure how much it's a bug and how much some kind of "undocumented behavior". environment: CentOS 7, Python 3.7.7, package installed with pip install . My code:


@contextmanager
def connection():
    bcp_connection = bcp.Connection(
        server="*", username="*", password="*", database="*", batchsize=16,
    )
    yield bcp_connection
    bcp_connection.disconnect()

def main():
    datasets = [
        [[1, 1], [2, 2]],
        [[5, 5], [6, 6]],
        [[1, 1], [2, 2]]  # this should cause a PK conflict
    ]
    with connection() as bcp_connection:
        for data in datasets:
            bcp_connection.init(TABLE)
            for row in data:
                bcp_connection.send(row)
            bcp_connection.commit()

if __name__ == "__main__":
    main()

Final result in database is:

1       1
2       2
35848246    35848246
1726742581  1726742581
35848242    35848242
1726742577  1726742577

Everything works as it should when I make a reconnection after each commit.

damionw commented 4 years ago

That seems like a bug, possibly provoked by reusing an existing bcp connection object. Try doing the context object within the for loop and see if it makes a difference. In the meantime, I'll try to reproduce in Python 2 and Python 3

On Fri, Mar 13, 2020 at 2:27 PM Grzegorz Chilczuk notifications@github.com wrote:

I'm not sure how much it's a bug and how much some kind of "undocumented behavior". environment: CentOS 7, Python 3.7.7, package installed with pip install . My code:

@contextmanagerdef connection(): bcp_connection = bcp.Connection( server="", username="", password="", database="", batchsize=16, ) yield bcp_connection bcp_connection.disconnect()

def main(): datasets = [ [[1, 1], [2, 2]], [[5, 5], [6, 6]], [[1, 1], [2, 2]] # this should cause a PK conflict ] with connection() as bcp_connection: for data in datasets: bcp_connection.init(TABLE) for row in data: bcp_connection.send(row) bcp_connection.commit()

if name == "main": main()

Final result in database is:

1 1 2 2 35848246 35848246 1726742581 1726742581 35848242 35848242 1726742577 1726742577

Everything works as it should when I make a reconnection after each commit.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/damionw/pythonbcp/issues/2, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQRXFDPTRLXB3CUETWIUWDRHJUHTANCNFSM4LHHAKQQ .

damionw commented 4 years ago

Also, what are the TDS and SQL Server versions?

gchilczuk commented 4 years ago

Freetds:

freetds.x86_64                         1.1.20-1.el7                    @epel    
freetds-devel.x86_64                   1.1.20-1.el7                    @epel    
freetds-libs.x86_64                    1.1.20-1.el7                    @epel    

SQL Server: Microsoft SQL Server 2016 (SP2-CU11-GDR) (KB4535706) - 13.0.5622.0 (X64)