You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
data-transport/notebooks/mssqlserver.ipynb

4.4 KiB

None <html lang="en"> <head> </head>

Writing to Microsoft SQLServer

  1. Insure the Microsoft SQL Server is installed and you have access i.e account information
  2. The target database must be created before hand.
  3. We created an authentication file that will contain user account and location of the database

The cell below creates a dataframe that will be stored in a Microsoft SQL Server database.

NOTE This was not tested with a cloud instance

In [ ]:
#
# Writing to Google Bigquery database
#
import transport
from transport import providers
import pandas as pd
import os

AUTH_FOLDER = os.environ['DT_AUTH_FOLDER'] #-- location of the service key
MSSQL_AUTH_FILE= os.sep.join([AUTH_FOLDER,'mssql.json'])

_data = pd.DataFrame({"name":['James Bond','Steve Rogers','Steve Nyemba'],'age':[55,150,44]})
msw = transport.get.writer(provider=providers.MSSQL,table='friends',auth_file=MSSQL_AUTH_FILE)
msw.write(_data,if_exists='replace') #-- default is append
print (['data transport version ', transport.__version__])

Reading from Microsoft SQL Server database

The cell below reads the data that has been written by the cell above and computes the average age within an MS SQL Server (simple query).

  • Basic read of the designated table (friends) created above
  • Execute an aggregate SQL against the table

NOTE

By design read object are separated from write objects in order to avoid accidental writes to the database. Read objects are created with transport.get.reader whereas write objects are created with transport.get.writer

In [ ]:
import transport
from transport import providers
import os
AUTH_FOLDER = os.environ['DT_AUTH_FOLDER'] #-- location of the service key
MSSQL_AUTH_FILE= os.sep.join([AUTH_FOLDER,'mssql.json'])

msr = transport.get.reader(provider=providers.MSSQL,table='friends',auth_file=MSSQL_AUTH_FILE)
_df = msr.read()
_query = 'SELECT COUNT(*) _counts, AVG(age) from friends'
_sdf = msr.read(sql=_query)
print (_df)
print ('\n--------- STATISTICS ------------\n')
print (_sdf)

An auth-file is a file that contains database parameters used to access the database. For code in shared environments, we recommend

  1. Having the auth-file stored on disk
  2. and the location of the file is set to an environment variable.

To generate a template of the auth-file open the file generator wizard found at visit https://healthcareio.the-phi.com/data-transport

In [1]:
{
    "provider":"sqlserver",
    "dataset":"demo","table":"friends","username":"<username>","password":"<password>"
}
Out[1]:
{'provider': 'sqlserver',
 'dataset': 'demo',
 'table': 'friends',
 'username': '<username>',
 'password': '<password>'}
In [ ]:

</html>