Skip to content

Commit

Permalink
inserts!
Browse files Browse the repository at this point in the history
  • Loading branch information
johnclary committed Sep 14, 2016
1 parent 2937334 commit 1bee106
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
SYSTEM_ID,BEFORE_AM_EB_NB_VOL,BEFORE_AM_EB_NB_TT,BEFORE_AM_EB_NB_STOPS,BEFORE_AM_WB_SB_VOL,BEFORE_AM_WB_SB_TT,BEFORE_AM_WB_SB_STOPS,BEFORE_OFF_EB_NB_VOL,BEFORE_OFF_EB_NB_TT,BEFORE_OFF_EB_NB_STOPS,BEFORE_OFF_WB_SB_VOL,BEFORE_OFF_WB_SB_TT,BEFORE_OFF_WB_SB_STOPS,BEFORE_PM_EB_NB_VOL,BEFORE_PM_EB_NB_TT,BEFORE_PM_EB_NB_STOPS,BEFORE_PM_WB_SB_VOL,BEFORE_PM_WB_SB_TT,BEFORE_PM_WB_SB_STOPS,AFTER_AM_EB_NB_TT,AFTER_AM_EB_NB_STOPS,AFTER_AM_WB_SB_TT,AFTER_AM_WB_SB_STOPS,AFTER_OFF_EB_NB_TT,AFTER_OFF_EB_NB_STOPS,AFTER_OFF_WB_SB_TT,AFTER_OFF_WB_SB_STOPS,AFTER_PM_EB_NB_TT,AFTER_PM_EB_NB_STOPS,AFTER_PM_WB_SB_TT,AFTER_PM_WB_SB_STOPS
11,0,300.6,1.6,0,348.5,2.6667,0,324.3333,2.6667,0,405.6667,3,0,381.5714,3.8571,0,473.8571,4.2857,302.6667,2.6667,317.6667,3,325,2.6667,335.3333,3,359.3333,3.6667,394,4.3333
19,0,685,6,0,792,7,0,766,6,0,918,7.5,0,987,7.8,0,1224,10.5,613,4,670,3,589,2.5,613,2.3,932,6,882,5
78,219,424.5,1,280,465,2,247,455.5,2,116,494.75,2.75,251,727.6667,6.3333,119,553.6667,3.3333,423.3333,2.3333,481,2.3333,420,2.3333,420,2.3333,502.3333,3.3333,530,3.3333
22,0,336,3,0,352,2,0,249,0,0,298,1,0,355,1.67,0,300,2,300,1,332,1.67,246,0,295,1,271,0,290,2
28,0,210,2,0,250,2.67,0,116,0,0,215,2.33,0,166,1,0,193,1.33,118,0,176,1,117,0,171,1,147,0.33,212,1.33
34,0,369.4,2.8,0,639.8,6.8,0,461.1667,4.3333,0,444.1667,3.5,0,767.6667,9.8333,0,488.6667,6,389,3,485,5.6667,369.3333,1.6667,432.3333,4,551,8.3333,459.3333,5
37,0,388.6667,3.3333,0,363.3333,3.3333,0,432.3333,3.6667,0,359,2.6667,0,1881,17.5,0,764,7,349.3333,3,440.6667,3.3333,483,4.3333,400.6667,5.3333,1115.5,10.5,1015,11.5
69,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
73,0,359.6667,6,0,229.3333,2.3333,0,470,6,0,428,6,0,420.6667,5.3333,0,281.3333,3.3333,242.75,2.5,262.75,3,235.3333,2.6667,217.3333,2,356,4.3333,203,1.3333
75,0,1096.5,7.25,0,1469.75,11.25,0,0,0,0,0,0,0,1444.5,12.5,0,1057,7.5,998.5,6.25,1094.25,7.5,0,0,0,0,1370.6667,10.6667,1055.5,6
75 changes: 75 additions & 0 deletions intersection-database/sync-systems/update_retimings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import pyodbc
import csv
from secrets import IDB_PROD_CREDENTIALS

SOURCE_FILE = 'source-data/retiming_import_sep2016.csv'

wait = 'N'

while wait != 'Y':
wait = raw_input('You are about to make changes to a production database. Press \'Y\' to continue.')
wait.upper()

def connect_db():
print('connecting to db')

conn = pyodbc.connect(
'DRIVER={{SQL Server}};'
'SERVER={};'
'PORT=1433;'
'DATABASE={};'
'UID={};'
'PWD={}'
.format(
IDB_PROD_CREDENTIALS['server'],
IDB_PROD_CREDENTIALS['database'],
IDB_PROD_CREDENTIALS['user'],
IDB_PROD_CREDENTIALS['password']
))

return conn

def get_csv_data(source_file):
print('getting csv data')

with open(source_file, 'r') as file:
reader = csv.DictReader(file)
reader = [row for row in reader]
return reader

def update_database(connection, payload):
print('updating {} records in database'.format(str(len(payload))))

updated = 0

cursor = connection.cursor()

for record in payload:

columns = str(record.keys()).translate(None, "[]'")

values = str(record.values()).translate(None, "[]")

statement = '''
INSERT INTO Access.RETIMING ({})
VALUES ({})
'''.format(columns, values)

print(statement)

cursor.execute(statement)

connection.commit()

updated += 1

return updated

conn = connect_db()

csv_data = get_csv_data(SOURCE_FILE)

results = update_database(conn, csv_data)

print('{} records updated'.format(str(results)))

0 comments on commit 1bee106

Please sign in to comment.