How to connect MySQL DB in Python?

How to connect MySQL DB in Python?

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.

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("") 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()

Relevant Projects

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.