-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.py
More file actions
30 lines (27 loc) · 1.12 KB
/
connection.py
File metadata and controls
30 lines (27 loc) · 1.12 KB
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
#Libraries
import csv
import MySQLdb
import sys
#Connecting to a MySQL database
#Connection credentials used will work only if you are connected over VPN. Create your setup and replace values for connection,
class MySQLConnection:
con = MySQLdb.connect(host='your_host',
port=3306,
user='your_username',
database='nyc_restaurants',
passwd='your_password')
c = con.cursor()
# Get the count
main_query = c.execute('''SELECT count(CAMIS) FROM DOHMH_New_York_City_Restaurant_Inspection_Results''')
# If the count is 1, then customers_dataset table already exists
row = c.fetchone()
if row != None : {
print('Connection successfull and records already exist.')
}
else:
# Load data from csv to db format
c.execute('''LOAD DATA INFILE '/var/lib/mysql/DOHMH_New_York_City_Restaurant_Inspection_Results.csv'
INTO TABLE `DOHMH_New_York_City_Restaurant_Inspection_Results`
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
IGNORE 1 ROWS;''')
print('Table created.')