-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (49 loc) · 2.13 KB
/
main.py
File metadata and controls
66 lines (49 loc) · 2.13 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
import boto3
import shutil
import openpyxl
import pymysql
from getpass import getpass
import csv
#Pymysql Function
def db_connection(hostname,username,password,db_name,port_number):
"""Connects to SQL DB using pymysql module and return the connection """
connection = pymysql.connect(host=hostname, user=username, passwd=password, db=db_name, port=port_number)
return(connection)
def example_sql_query(hostname,username,password,db_name,port_number):
"""This is a example function which will query * in a specific table"""
#Create a empty list of data
sql_data = []
#creating connection to the database
connection = db_connection(hostname,username,password,db_name,port_number)
#Setting the cursor
cursor = connection.cursor()
#Execute the SQL Query using execute() method, replace db_table_here with your db_table #
#Or SELECT specific columns
#Or add your specific SQL query
#Or pass in variables of your own
cursor.execute ("""SELECT * FROM db_table_here""")
#fetching all records
data = cursor.fetchall()
return sorted(sql_data)
def csv_report_creator(sql_data):
#Creates a csv file with all your data
with open('my_report.csv', 'w', newline='') as f_handle:
writer = csv.writer(f_handle)
#Add the header/column names
header = ['make', 'style', 'color', 'plate']
writer.writerow(header)
#Iterate over data and write to the csv file
for row in sql_data:
writer.writerow(row)
def main():
#print('This is example script using a already created SQL Database\n\n')
username = input('Please enter your username for your Database: ')
password = getpass(prompt='Database Password: ')
db_name = input('Database DNS Name: ')
port_number = input ('Database Port number (1521 or 3306 or custom number): ')
#convert port_number into integer
port_number = int(port_number)
# Executing Example Jobs
sql_query_job = example_sql_query(hostname,username,password,db_name,port_number)
#Creating CSV File
csv_report_creator(sql_query_job)