Featured Post

Python Set Operations Explained: From Theory to Real-Time Applications

Image
A  set  in Python is an unordered collection of unique elements. It is useful when storing distinct values and performing operations like union, intersection, or difference. Real-Time Example: Removing Duplicate Customer Emails in a Marketing Campaign Imagine you are working on an email marketing campaign for your company. You have a list of customer emails, but some are duplicated. Using a set , you can remove duplicates efficiently before sending emails. Code Example: # List of customer emails (some duplicates) customer_emails = [ "alice@example.com" , "bob@example.com" , "charlie@example.com" , "alice@example.com" , "david@example.com" , "bob@example.com" ] # Convert list to a set to remove duplicates unique_emails = set (customer_emails) # Convert back to a list (if needed) unique_email_list = list (unique_emails) # Print the unique emails print ( "Unique customer emails:" , unique_email_list) Ou...

How to Call SQL Query from Python

Python's top supported database is MySQL. You can run SQL queries from Python. Here're best examples of how to connect to MYSQL and access MYSQL tables from Python.


Read: How to Print String in Next Line Easily


Here are Steps

  1. Import MySQL connecter
  2. Give user id, password details
  3. Issue SQL query


Python Logic to import MySQL connector


import mysql.connector


Note: If the MySQL connecter not installed in python, you need to install it using the below command.


pip3 install mysql-connector-python --allow-external mysql-connector-python


How to Call SQL Query from Python



Supply user id and Password


conn=mysql.connector.connect(user='root', password='password', host='localhost', database='sakila')



mycursor=conn.cursor()


Issue SQL Query



mycursor.execute("show tables") # you won't see any result. You need to give print.


print(mycursor.fetchall())


mycursor.execute("select * from customer") # you won't see any result. You need to give print.


print(mycursor.fetchall())


This way you can issue SQL queries in Python.


Read: Top 100 Hadoop Interview Questions 1 of 4

Comments

Popular posts from this blog

SQL Query: 3 Methods for Calculating Cumulative SUM

Big Data: Top Cloud Computing Interview Questions (1 of 4)

5 SQL Queries That Popularly Used in Data Analysis