Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mysql.connector
- from mysql.connector import Error
- def connect_SQL_EMC_database():
- try:
- #Connecting to the database
- mySQLconnection = mysql.connector.connect(host='sql9.freesqldatabase.com',
- database='sql9298674',
- user='sql9298674',
- password='KR5i4m6mya')
- sql_select_data = "select * from EMC_data" #to fetch all rows from the database
- data_cursor = mySQLconnection.cursor() #Creating a cursor to point to all entries in the database
- data_cursor.execute(sql_select_data) #returns a ResultSet object which contains all the rows
- data_records = data_cursor.fetchall()
- sql_select_regions = "select * from EMC_regions" #to fetch all rows from the database
- region_cursor = mySQLconnection.cursor() #Creating a cursor to point to all entries in the database
- region_cursor.execute(sql_select_regions) #returns a ResultSet object which contains all the rows
- region_records = region_cursor.fetchall()
- print("Total number of rows in EMC_data is - ", data_cursor.rowcount)
- print ("Printing each row's column values ")
- for row in data_records:
- print("CUST_ID = ", row[0], )
- print("DATE = ", row[1])
- print("ORDER_QTY = ", row[2])
- print("PRICE_USD = ", row[3])
- print("Region_CODE = ", row[4], "\n")
- data_cursor.close()
- print("Total number of rows in EMC_regions is - ", region_cursor.rowcount)
- print ("Printing each row's column values ")
- for row in region_records:
- print("REGION = ", row[0], )
- print("Region_CODE = ", row[1], "\n")
- region_cursor.close()
- except Error as e :
- print ("Error while connecting to MySQL", e)
- finally:
- #closing database connection.
- if(mySQLconnection .is_connected()):
- connection.close()
- print("MySQL connection is closed")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement