Using Python Pandas dataframe to read and insert data to Microsoft SQL Server
Using Python Pandas dataframe to read and insert data to Microsoft SQL Server.
You can follow the steps below to clone the repository.
git clone https://github.com/tomaztk/MSSQLSERVER_Pandas.git
-- sample table
SELECT TOP 10
name
,object_id
FROM sys.tables
EXECUTE sp_execute_external_script @language = N'Python'
,@script = N'
import pandas as pd
OutputDataSet = pd.DataFrame(InputDataSet);
'
, @input_data_1 = N'SELECT TOP 10 name,object_id FROM sys.tables'
WITH RESULT SETS((
[Name] VARCHAR(150) NOT NULL
,[object_ID] CHAR(20) NOT NULL
));
import pandas as pd
import pyodbc
sql_conn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server}; \
SERVER=SQLSERVER2017;DATABASE=master;Trusted_Connection=yes')
query = "SELECT * FROM sys.tables"
df = pd.read_sql(query, sql_conn)
df.head(3)
Contributions of any kind is highly appreciated! Fork the repository, add your code.
Feel free to get in touch for questions regarding Python and MSSQL Server connectivity.