-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexExchDataBaseConn.py
More file actions
67 lines (61 loc) · 2.16 KB
/
indexExchDataBaseConn.py
File metadata and controls
67 lines (61 loc) · 2.16 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
""""
By: Drew Reid
Date: May 2, 2015
Purpose:
class to make connection to sql
"""
#import custom Config class
from indexExchConfig import db_Config
#import installed connector provided by SQL
import mysql.connector
#import error codesf ro the connector provided by SQL
from mysql.connector import errorcode
#import the csv file layout
import csv
class sqlConn:
db_Conn = None
db_Cur = None
db_ConfigInfo = None
#When class is initialized create a database connection
def __init__(self, configFile):
#create a Config connection to get the database connection details
self.db_ConfigInfo = db_Config(configFile)
db_Login = self.db_ConfigInfo.read_db_Conn()
try: #to setup connection to local database
self.db_Conn = mysql.connector.connect(**db_Login)
self.db_Cur = self.db_Conn.cursor()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
self.db_Conn.close()
else:
print("Connection to the database successful...")
#function to query the connected database and return the results
def query(self, query):
try:
return self.db_Cur.execute(query)
except mysql.connector.Error as err:
print(err)
#create a file for uploading purposes
def open_db_file(self, file):
#Get the upload file details from the config file
db_Path = self.db_ConfigInfo.read_db_Path()+file
try:
#try to create or delete records already in the upload file
return open(db_Path, "w")
except:
print("Error opening up file, "+ValueError)
#commit the rows in the upload file to the database table
def commit_csvfile_to_table(self, file, table):
try:
self.db_Cur.execute("LOAD DATA INFILE '"+file+"' INTO TABLE "+table+" FIELDS ENCLOSED BY '"+'"'+"' ESCAPED BY '"+'"'+"'"+" TERMINATED BY ',' LINES TERMINATED BY '\r\n'")
#commit the above command to the database
self.db_Conn.commit()
except mysql.connector.Error as err:
print(err)
def __del__(self):
self.db_Conn.close()