Featured Post

14 Top Data Pipeline Key Terms Explained

Image
 Here are some key terms commonly used in data pipelines 1. Data Sources Definition: Points where data originates (e.g., databases, APIs, files, IoT devices). Examples: Relational databases (PostgreSQL, MySQL), APIs, cloud storage (S3), streaming data (Kafka), and on-premise systems. 2. Data Ingestion Definition: The process of importing or collecting raw data from various sources into a system for processing or storage. Methods: Batch ingestion, real-time/streaming ingestion. 3. Data Transformation Definition: Modifying, cleaning, or enriching data to make it usable for analysis or storage. Examples: Data cleaning (removing duplicates, fixing missing values). Data enrichment (joining with other data sources). ETL (Extract, Transform, Load). ELT (Extract, Load, Transform). 4. Data Storage Definition: Locations where data is stored after ingestion and transformation. Types: Data Lakes: Store raw, unstructured, or semi-structured data (e.g., S3, Azure Data Lake). Data Warehous...

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

How to Fix datetime Import Error in Python Quickly

SQL Query: 3 Methods for Calculating Cumulative SUM

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