-
-
Save kyle-krieger/856c6aca0a19c1a3b14f8199626d9966 to your computer and use it in GitHub Desktop.
This code will create a dataframe within Python to then be used to make a table within a specified SQL database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from pathlib import Path | |
import pyodbc | |
path = r'YOUR PATH HERE' | |
files = Path(path).glob('*.csv') | |
dfs = list() | |
for f in files: | |
data = pd.read_csv(f) | |
data['file'] = f.stem | |
dfs.append(data) | |
df = pd.concat(dfs, ignore_index=True) | |
try: | |
cnxn = pyodbc.connect("Driver={YOUR ODBC DRIVER HERE};Server=YOUR SERVER;Database=YOUR DATABASE;Trusted_Connection=yes") | |
cursor = cnxn.cursor() | |
try: | |
cursor.execute(''' | |
CREATE TABLE divvy_trips ( | |
[ride_id] NVARCHAR(255), | |
[rideable_type] NVARCHAR(255), | |
[started_at] DATETIME, | |
[ended_at] DATETIME, | |
[start_time] TIME, | |
[end_time] TIME, | |
[ride_length] FLOAT, | |
[Weekday] INT, | |
[member_casual] NVARCHAR(255) | |
) | |
''') | |
for row in df.itertuples(): | |
cursor.execute(''' | |
INSERT INTO divvy_trips ( | |
ride_id, rideable_type, started_at, ended_at, start_time, end_time, | |
ride_length, Weekday, member_casual | |
) | |
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) | |
''', ( | |
row.ride_id, | |
row.rideable_type, | |
row.started_at, | |
row.ended_at, | |
row.start_time, | |
row.end_time, | |
row.ride_length, | |
row.Weekday, | |
row.member_casual | |
)) | |
cnxn.commit() | |
except Exception as ex: | |
print("Error adding data to table:", ex) | |
finally: | |
cursor.close() | |
except Exception as ex: | |
print("Error connecting to database:", ex) | |
finally: | |
cnxn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment