Skip to content

Commit db00b26

Browse files
committed
Created using Colaboratory
1 parent e1ed28b commit db00b26

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

Python_MySQL_P2.ipynb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"authorship_tag": "ABX9TyOGWK6dnDixpCO5rfMXEVXx",
8+
"include_colab_link": true
9+
},
10+
"kernelspec": {
11+
"name": "python3",
12+
"display_name": "Python 3"
13+
},
14+
"language_info": {
15+
"name": "python"
16+
}
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {
22+
"id": "view-in-github",
23+
"colab_type": "text"
24+
},
25+
"source": [
26+
"<a href=\"https://colab.research.google.com/github/Animeshcoder/MySQL-Python/blob/main/Python_MySQL_P2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"source": [
32+
"### **Introduction**\n",
33+
"\n",
34+
"This project is a Python script that demonstrates how to extract data from one MySQL database, manipulate it using Pandas, and save it to another MySQL database. The script uses Pandas, SQLAlchemy, and PyMySQL to connect to the databases, execute SQL queries, and transfer data between them. The extracted data is stored in a Pandas DataFrame, which provides powerful data manipulation capabilities and allows for easy integration with other data analysis tools. This project can serve as a starting point for building more complex data processing pipelines that involve extracting, transforming, and loading data between different databases."
35+
],
36+
"metadata": {
37+
"id": "DntCJmSqKVj1"
38+
}
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"metadata": {
44+
"id": "OBxNk2jtKS4d"
45+
},
46+
"outputs": [],
47+
"source": [
48+
"import pandas as pd\n",
49+
"from sqlalchemy import create_engine\n",
50+
"import urllib.parse\n",
51+
"\n",
52+
"password = \"yourpassword@123\"\n",
53+
"password = urllib.parse.quote(password)\n",
54+
"\n",
55+
"engine = create_engine(f\"mysql+pymysql://youruser:{password}@yourhost/olddatabase\")\n",
56+
"\n",
57+
"# write a SQL query to extract the data you need\n",
58+
"query = \"\"\"\n",
59+
" SELECT Value\n",
60+
" FROM databasename.tablename\n",
61+
" WHERE ID IN ('1','2','3') and Value is not null\n",
62+
"\"\"\"\n",
63+
"\n",
64+
"# use the read_sql_query function from pandas to execute the query and store the result in a DataFrame\n",
65+
"df = pd.read_sql_query(query, engine)\n",
66+
"\n",
67+
"# create a connection to the new database\n",
68+
"new_engine = create_engine(f\"mysql+pymysql://youruser:{password}@yourhost/newdatabase\")\n",
69+
"\n",
70+
"# save the data to the new database\n",
71+
"df.to_sql(\"newtable\", new_engine, index=False, if_exists=\"append\")"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"source": [
77+
"### **Explanation:**\n",
78+
"\n",
79+
"**Dependencies:** The code starts by importing the required dependencies, including the pandas module (aliased as pd), the create_engine function from the sqlalchemy module, and the urllib.parse module.\n",
80+
"\n",
81+
"**Password encoding:** The code defines a password variable that contains the password for the MySQL server. This password is then URL-encoded using the quote function from the urllib.parse module to ensure that it can be safely included in a connection string.(You don't need to do this step if your password does not have **@** in-between. You can use that directly)\n",
82+
"\n",
83+
"**Create engine:** The code uses the create_engine function from SQLAlchemy to create an engine object that represents a connection to the old MySQL database. This is done by calling create_engine with a connection string that specifies the dialect (mysql+pymysql), username (youruser), password (the URL-encoded password), host (yourhost), and database name (olddatabase) of the MySQL server.\n",
84+
"\n",
85+
"**Define query:** The code defines an SQL query that selects the Value column from a table named tablename in a database named databasename, where the ID column is in the list of values ('1', '2', '3') and the Value column is not null. This query is used to extract the data that will be saved to the new database.\n",
86+
"\n",
87+
"**Execute query:** The code uses the read_sql_query function from Pandas to execute the SQL query on the old MySQL database and store the result in a DataFrame. This is done by calling read_sql_query with the SQL query and engine object as arguments.\n",
88+
"\n",
89+
"**Create new engine:** The code uses the create_engine function from SQLAlchemy again to create another engine object that represents a connection to the new MySQL database. This is done in the same way as before, but with a different database name (newdatabase) in the connection string.\n",
90+
"\n",
91+
"**Save data:** The code uses the to_sql method of the DataFrame object to save its data to a new table named newtable in the new MySQL database. This is done by calling to_sql with the table name, new engine object, and additional arguments such as index=False (to prevent saving the DataFrame’s index as a column) and if_exists='append' (to append the data to an existing table if it already exists) as arguments."
92+
],
93+
"metadata": {
94+
"id": "cjFFPbHnMVSZ"
95+
}
96+
}
97+
]
98+
}

0 commit comments

Comments
 (0)