-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMSSQL_Pandas.py
48 lines (33 loc) · 1.2 KB
/
MSSQL_Pandas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
Author: Tomaz Kastrun
Date: 12.07.2018
get data from sql server to pandas
get data from pandas data frame to sql server database
'''
## From SQL to DataFrame Pandas
import pandas as pd
import pyodbc
sql_conn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=SQLSERVER2017;DATABASE=Adventureworks;Trusted_Connection=yes')
query = "SELECT [BusinessEntityID],[FirstName],[LastName],[PostalCode],[City] FROM [Sales].[vSalesPerson]"
df = pd.read_sql(query, sql_conn)
df.head(3)
## From DataFrame Pandas to SQL
'''
Have table prepared ON Microsoft SQL Server
USE AdventureWorks;
GO
DROP TABLE IF EXISTS vSalesPerson_test
CREATE TABLE vSalesPerson_test
(
[BusinessEntityID] INT
,[FirstName] VARCHAR(50)
,[LastName] VARCHAR(100)
)
'''
connStr = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};SERVER=SQLSERVER2017;DATABASE=Adventureworks;Trusted_Connection=yes')
cursor = connStr.cursor()
for index,row in df.iterrows():
cursor.execute("INSERT INTO dbo.vSalesPerson_test([BusinessEntityID],[FirstName],[LastName]) values (?, ?,?)", row['BusinessEntityID'], row['FirstName'] , row['LastName'])
connStr.commit()
cursor.close()
connStr.close()