How to connect MySQL DB in Python?

This recipe helps you connect MySQL DB in Python

Recipe Objective

Have you tried to connect MySQL DB in python ?

So this is the recipe on how we can connect MySQL DB in Python.

Access YOLO OCR Character Recognition Project with Source Code

Step 1 - Importing Libraries

import sqlalchemy as sa import pandas as pd

Here we have improted pandas and sqlalchemy which will be needed.

Step 2 - Connecting to SQL

We have first used iris dataset for this and passed the information of SQL and applied dataset.to_sql to save the file as SQL. dataset = pd.read_csv("iris.data.csv") engine_str = ( "mysql+pymysql://{user}:{password}@{server}/{database}".format( user = "root", password = "root888", server = "localhost", database = "datasciencerecipes")) engine = sa.create_engine(engine_str) conn = engine.connect() # check whether connection is Successful or not if (conn): print("MySQL Connection is Successful ... ... ...") else: print("MySQL Connection is not Successful ... ... ...") dataset.to_sql(name="irisdata", con=engine, schema="datasciencerecipes", if_exists = "replace", chunksize = 1000, index=False) conn.close()

 

Download Materials

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Linear Regression Model Project in Python for Beginners Part 2
Machine Learning Linear Regression Project for Beginners in Python to Build a Multiple Linear Regression Model on Soccer Player Dataset.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Build Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed