Skip to main content

Tool for importing data to MS SQL Server.

Project description

Python module for uploading numpy arrays to MS SQL Server

This module allows dictionaries of numpy arrays to be uploaded to SQL Server as database tables. The upload is carried out as a bulk insert, which copies binary data to the server without any conversion to or from text. This is faster than uploading text and exactly preserves floating point values.

Compilation

The module uses freetds to communicate with the database server. To build the module, set the location of the freetds library and headers and then run pip install. E.g.:

SYBDB=/cosma/local/freetds/1.4.20/ LDFLAGS="-L${SYBDB}/lib/ -Wl,-rpath=${SYBDB}/lib/" CFLAGS="-I${SYBDB}/include/" pip install .

Usage

Connect to the database:

dbcon = pybcp.DBConnection(username, server, database, password=None)

Here, username and password are the database username and password. server is the server name defined in freetds.conf and database is the name of the database which contains the table to be created or updated.

If the password is None then a password prompt will appear.

Uploading a database table

dbcon.bulk_insert(table_name, columns, create_table=True, drop_existing=True)

Here, table_name is the name of the table to create or append to. columns is a dict of numpy arrays with the data for each column. If create_table is true then a new table will be created. If drop_existing is true then any existing table is removed first.

Table column arrays should be stored in an OrderedDict if the ordering of the columns is important.

Executing SQL commands

For convenience there's a function to execute SQL commands:

dbcon.execute(sql)

where sql is a string with the command to execute. This can be useful for creating indexes on newly uploaded tables, for example.

This returns a list containing any tables returned by the command. Any returned tables are represented as dictionaries of arrays in the same way as data to be bulk inserted.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pybcp-0.1.3.tar.gz (23.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybcp-0.1.3-cp312-cp312-manylinux_2_34_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

File details

Details for the file pybcp-0.1.3.tar.gz.

File metadata

  • Download URL: pybcp-0.1.3.tar.gz
  • Upload date:
  • Size: 23.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for pybcp-0.1.3.tar.gz
Algorithm Hash digest
SHA256 0245349a26b3c4b496c852d76a8948eb436c0d1d1a52b8305dd9aed9f906a9a3
MD5 47c6dc62c860adab4d7c5c27be48affd
BLAKE2b-256 dca970a3a6976071fec6d1532916e108554d27c11060d867f40c719fdb4bbcb3

See more details on using hashes here.

File details

Details for the file pybcp-0.1.3-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pybcp-0.1.3-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 dad8c89091cfadd1aa9b1491db0a559019487c78ce055282de7596a0677222ab
MD5 f94c43d87447c5be917c442466e459b0
BLAKE2b-256 68bf10bbd29b49fb7cb47080dd307d44f646f34a77437627399789212aebe52d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page