{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### Writing to Microsoft SQLServer\n", "\n", "1. Insure the Microsoft SQL Server is installed and you have access i.e account information\n", "2. The target database must be created before hand.\n", "3. We created an authentication file that will contain user account and location of the database\n", "\n", "The cell below creates a dataframe that will be stored in a Microsoft SQL Server database.\n", "\n", "**NOTE** This was not tested with a cloud instance" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#\n", "# Writing to Google Bigquery database\n", "#\n", "import transport\n", "from transport import providers\n", "import pandas as pd\n", "import os\n", "\n", "AUTH_FOLDER = os.environ['DT_AUTH_FOLDER'] #-- location of the service key\n", "MSSQL_AUTH_FILE= os.sep.join([AUTH_FOLDER,'mssql.json'])\n", "\n", "_data = pd.DataFrame({\"name\":['James Bond','Steve Rogers','Steve Nyemba'],'age':[55,150,44]})\n", "msw = transport.get.writer(provider=providers.MSSQL,table='friends',auth_file=MSSQL_AUTH_FILE)\n", "msw.write(_data,if_exists='replace') #-- default is append\n", "print (['data transport version ', transport.__version__])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Reading from Microsoft SQL Server database\n", "\n", "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). \n", "\n", "- Basic read of the designated table (friends) created above\n", "- Execute an aggregate SQL against the table\n", "\n", "**NOTE**\n", "\n", "By design **read** object are separated from **write** objects in order to avoid accidental writes to the database.\n", "Read objects are created with **transport.get.reader** whereas write objects are created with **transport.get.writer**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "import transport\n", "from transport import providers\n", "import os\n", "AUTH_FOLDER = os.environ['DT_AUTH_FOLDER'] #-- location of the service key\n", "MSSQL_AUTH_FILE= os.sep.join([AUTH_FOLDER,'mssql.json'])\n", "\n", "msr = transport.get.reader(provider=providers.MSSQL,table='friends',auth_file=MSSQL_AUTH_FILE)\n", "_df = msr.read()\n", "_query = 'SELECT COUNT(*) _counts, AVG(age) from friends'\n", "_sdf = msr.read(sql=_query)\n", "print (_df)\n", "print ('\\n--------- STATISTICS ------------\\n')\n", "print (_sdf)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An **auth-file** is a file that contains database parameters used to access the database. \n", "For code in shared environments, we recommend \n", "\n", "1. Having the **auth-file** stored on disk \n", "2. and the location of the file is set to an environment variable.\n", "\n", "To generate a template of the **auth-file** open the **file generator wizard** found at visit https://healthcareio.the-phi.com/data-transport" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'provider': 'sqlserver',\n", " 'dataset': 'demo',\n", " 'table': 'friends',\n", " 'username': '',\n", " 'password': ''}" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "{\n", " \"provider\":\"sqlserver\",\n", " \"dataset\":\"demo\",\"table\":\"friends\",\"username\":\"\",\"password\":\"\"\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 2 }