You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"<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."
"**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."
0 commit comments