{"id":61343,"date":"2018-11-16T11:00:36","date_gmt":"2018-11-16T09:00:36","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=61343"},"modified":"2018-11-15T11:22:31","modified_gmt":"2018-11-15T09:22:31","slug":"mysql-python-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/","title":{"rendered":"MySQL Python Example"},"content":{"rendered":"<h2 id=\"introduction\">1. MySQL Python &#8211; Introduction<\/h2>\n<p>In this post, we feature a comprehensive Tutorial on integrating MySQL in a simple <a href=\"https:\/\/www.javacodegeeks.com\/minibook\/python-programming-cookbook\" target=\"_blank\" rel=\"noopener\">Python<\/a> application based on <a href=\"http:\/\/flask.pocoo.org\/\" target=\"_blank\" rel=\"noopener\">Flask<\/a> and execute basic CRUD operations with the same.\u00a0<a href=\"https:\/\/www.mysql.com\/\" target=\"_blank\" rel=\"noopener\">MySQL<\/a>\u00a0is one of the most used SQL databases in many of the world-enterprise applications due to its ability to manage transactions and the ACID-behaviour which is built into its core. One of the reasons MySQL is so popular is due to the easiness it provides for its usage and its related commands. In the open source web application framework LAMP (which consist of Linux, Apache, MySQL, and PHP),\u00a0MySQL server is a central &amp; important component. The MySQL Database server is written using C and C++ which internally uses\u00a0a lexical analyzer to parse and understand the SQL queries.<\/p>\n<p>For this lesson, we need to have a complete MySQL Server installation so that we can run examples for the Python application we create. Please go through\u00a0the\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/enterprise-java\/sql-enterprise-java\/mysql-server-tutorial\/\" target=\"_blank\" rel=\"noopener\">MySQL Server Tutorial<\/a>\u00a0lesson to understand how installation can be done with some simple commands to get started with the MySQL operations in a <a href=\"https:\/\/www.javacodegeeks.com\/2016\/03\/10-awesome-python-tutorials-kick-start-projects.html\" target=\"_blank\" rel=\"noopener\">Python<\/a> application. We will be using a <a href=\"https:\/\/virtualenv.pypa.io\/en\/latest\/\" target=\"_blank\" rel=\"noopener\">Virtual environment<\/a> to install flask so that we don&#8217;t have to use it globally on our machine. Please note that to complete and follow along with this lesson, you must install the <code>virtualenv<\/code> and Flask on your machine.<\/p>\n<div class=\"toc\">\n<h3>Table Of Contents<\/h3>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#setup_python\">2. Setting up Python application<\/a><\/dt>\n<dt><a href=\"#setup_app_routes\">3. Making routes in Flask application<\/a><\/dt>\n<dt><a href=\"#add_mysql\">4. Adding MySQL dependencies and parameters<\/a><\/dt>\n<dt><a href=\"#insert\">5. Insert new records<\/a><\/dt>\n<dt><a href=\"#select\">6. Display data from MySQL<\/a><\/dt>\n<dt><a href=\"#update\">7. Update existing records in MySQL<\/a><\/dt>\n<dt><a href=\"#delete\">8. Delete data in MySQL<\/a><\/dt>\n<dt><a href=\"#conclusion\">9. Conclusion<\/a><\/dt>\n<dt><a href=\"#download\">10. Download the Source Code<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2 id=\"setup_python\">2. Setting up Python application<\/h2>\n<p>In this section, we will start by setting up a simple Python application\u00a0with important techniques to establish and close the connection with the server along with database connection pooling techniques which is very important to have in any production-grade application. In this lesson, we will make use of the virtual environment to use Flask in our application. Make sure that virtual environment is installed on your machine. We used the following command to install it via Python and PIP:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Install virtualenv<\/em><\/span><\/p>\n<pre class=\"brush:bash\">python -m pip install --user virtualenv<\/pre>\n<p>Now that <code>virtualenv<\/code> is installed on your machine, it is time we make a new <code>virtualenv<\/code> environment on our machine:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Make new virtual environment<\/em><\/span><\/p>\n<pre class=\"brush:bash\">python -m virtualenv jcg-python-mysql-app<\/pre>\n<p>Once a new Virtual environment is made as <code>jcg-python-mysql-app<\/code>, we will see the following output, describing that the virtual environment setup some things for us in advance which helps us to install more tools and softwares inside it:<\/p>\n<p><figure id=\"attachment_61391\" aria-describedby=\"caption-attachment-61391\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/new-virtual-environment.png\"><img decoding=\"async\" class=\"wp-image-61391 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/new-virtual-environment.png\" alt=\"MySQL Python - Creating new virtual environment\" width=\"820\" height=\"204\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/new-virtual-environment.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/new-virtual-environment-300x75.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/new-virtual-environment-768x191.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61391\" class=\"wp-caption-text\">Creating new virtual environment<\/figcaption><\/figure><\/p>\n<p>We can now start using this virtual environment with the following command:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Activate virtual environment<\/em><\/span><\/p>\n<pre class=\"brush:bash\">source jcg-python-mysql-app\/bin\/activate<\/pre>\n<p>We can also deactivate a virtual environment once we are done with its use like this:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Deactivate virtual environment<\/em><\/span><\/p>\n<pre class=\"brush:bash\">deactivate<\/pre>\n<p>We will see the following cursor change to confirm that a virtual environment is active now:<\/p>\n<p><figure id=\"attachment_61563\" aria-describedby=\"caption-attachment-61563\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/virtual-environment-active-deactivate.png\"><img decoding=\"async\" class=\"wp-image-61563 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/virtual-environment-active-deactivate.png\" alt=\"MySQL Python - Activate virtual environment\" width=\"820\" height=\"160\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/virtual-environment-active-deactivate.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/virtual-environment-active-deactivate-300x59.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/virtual-environment-active-deactivate-768x150.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61563\" class=\"wp-caption-text\">Activate virtual environment<\/figcaption><\/figure><\/p>\n<p>Now, when our virtual environment is active, we will install Flask framework in it so that we can use it in our application:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Install Flask<\/em><\/span><\/p>\n<pre class=\"brush:bash\">pip install Flask<\/pre>\n<p>Finally, we will install MySQL driver for Flask with the following command so that we can integrate MySQL in our application as well:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Install MySQL driver for Flask<\/em><\/span><\/p>\n<pre class=\"brush:bash\">pip install flask-mysql<\/pre>\n<p>With this command, we are ready to start making our application for Flask and MySQL integration. As our application is pretty simple, we will make a new file with name <code>mysql_app.py<\/code> and put our complete code in that file itself. Even though we will make use of a Python framework known as Flask to make this application, we could have used any other framework with very similar working. Other frameworks can be Django etc. or we can make a standalone Python application as well which is not bound to any framework. But for now, to keep things simple, we will make use of Flask which allows us to keep things simple.<\/p>\n<h2 id=\"setup_app_routes\">3. Making routes in Flask application<\/h2>\n<p>To start quickly for demonstration, we will add minor code in our application and run a set of commands which will start the flask application with a traditional &#8220;Hello World&#8221; message. If you want to quickly understand what a route is, just understand that a &#8216;route&#8217; allows us to call a Python function with the help of a URL. When we deploy a Python application on our local machine, this URL points to our local web server we make use of. In our case, we will make use of Flask&#8217;s embedded web server which runs on Port 5000 by default. Here is the code we will be using in our application for a simple traditional message:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Hello World from Flask<\/em><\/span><\/p>\n<pre class=\"brush:bash\">from flask import Flask\r\napp = Flask(__name__)\r\n\r\n@app.route('\/')\r\ndef hello_world():\r\n    return 'Hello, Geeks!'\r\n<\/pre>\n<p>Once we save this code, we can run a set of commands to run the application. Here is what we need to run:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Run Flask application<\/em><\/span><\/p>\n<pre class=\"brush:bash\">export FLASK_APP=mysql_app.py\r\nflask run\r\n<\/pre>\n<p>We will see the following output on terminal:<\/p>\n<p><figure id=\"attachment_61396\" aria-describedby=\"caption-attachment-61396\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-world.png\"><img decoding=\"async\" class=\"wp-image-61396 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-world.png\" alt=\"MySQL Python - Flask hello World\" width=\"820\" height=\"213\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-world.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-world-300x78.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-world-768x199.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61396\" class=\"wp-caption-text\">Flask hello World<\/figcaption><\/figure><\/p>\n<p>When we make use of a REST client like <a href=\"https:\/\/www.getpostman.com\/\" target=\"_blank\" rel=\"noopener\">Postman<\/a> to make the request to provided path, we will see the following output:<\/p>\n<p><figure id=\"attachment_61398\" aria-describedby=\"caption-attachment-61398\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-geeks.png\"><img decoding=\"async\" class=\"wp-image-61398 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-geeks.png\" alt=\"MySQL Python - Hello Geeks\" width=\"820\" height=\"539\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-geeks.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-geeks-300x197.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/flask-hello-geeks-768x505.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61398\" class=\"wp-caption-text\">Hello Geeks<\/figcaption><\/figure><\/p>\n<p>Now, we will add some routes for each of the CRUD operations we will performing in our Python application. These routes will look like:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><span style=\"text-decoration: underline;\"><em>Routes for CRUD operations<\/em><\/span><\/p>\n<pre class=\"brush:bash\">from flask import Flask, request\r\napp = Flask(__name__)\r\n\r\n@app.route('\/hello')\r\ndef hello_world():\r\n    return 'Hello, World!'\r\n\r\n@app.route('\/student', methods=['POST'])\r\ndef insert_student():\r\n    new_student = request.get_json()\r\n    return 'Insert Student data: %s' % new_student['name']\r\n\r\n@app.route('\/student\/&lt;int:student_id&gt;', methods=['GET'])\r\ndef get_student(student_id):\r\n    return 'Get Student data with ID: %s' % student_id\r\n\r\n@app.route('\/student', methods=['PUT'])\r\ndef update_student():\r\n    updated_student = request.get_json()\r\n    return 'Update Student data: %s' % updated_student['name']\r\n\r\n@app.route('\/student\/&lt;int:student_id&gt;', methods=['DELETE'])\r\ndef delete_student(student_id):\r\n    return 'Delete Student data with ID: %s' % student_id\r\n<\/pre>\n<p>Once we run the above application again and try to get POST some JSON in the insert student route, we will see the following output in Postman app:<\/p>\n<p><figure id=\"attachment_61408\" aria-describedby=\"caption-attachment-61408\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/python-example-insert-data.png\"><img decoding=\"async\" class=\"wp-image-61408 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/python-example-insert-data.png\" alt=\"MySQL Python - Example POST call with JSON\" width=\"820\" height=\"768\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/python-example-insert-data.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/python-example-insert-data-300x281.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/python-example-insert-data-768x719.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61408\" class=\"wp-caption-text\">Example POST call with JSON<\/figcaption><\/figure><\/p>\n<p>As this is not a Flask based lesson, we won&#8217;t dive into much detail about how we are accepting JSON data in above defined routes and how we access tge attributes which are passed in the request object as JSON data. This is a nice place to start a Flask application in a development mode because that allows us to quickly make changes to the application without having to restart your app again and again. Run the following command to activate the debugger and live reload in your Flask application:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Run Flask application in Development mode<\/em><\/span><\/p>\n<pre class=\"brush:bash\">export FLASK_APP=mysql_app.py FLASK_ENV=development\r\nflask run\r\n<\/pre>\n<p>Once this is done, we are free to make any changes in our application without having to restart our app again and again. Let&#8217;s move forward to integrating some MySQL based dependencies in our application.<\/p>\n<h2 id=\"add_mysql\">4. Adding MySQL dependencies and parameters<\/h2>\n<p>In the last section, we completed our setup of the basic Flask application in which we will integrate some MySQL related operations like saving and retrieving Student related data. Now, we need to define MySQL connection properties in our application with which we can establish a connection and execute MySQL properties with the connection. Here, we define those connection properties:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Define connection properties<\/em><\/span><\/p>\n<pre class=\"brush:bash\">from flask import Flask, request\r\nfrom flaskext.mysql import MySQL\r\n\r\napp = Flask(__name__)\r\nmysql = MySQL()\r\n\r\napp.config['MYSQL_DATABASE_USER'] = 'root'\r\napp.config['MYSQL_DATABASE_PASSWORD'] = 'root'\r\napp.config['MYSQL_DATABASE_DB'] = 'jcg_schema'\r\napp.config['MYSQL_DATABASE_HOST'] = 'localhost'\r\nmysql.init_app(app)\r\n<\/pre>\n<p>Now that we have defined the connection properties for MySQL connection with the <code>jcg_schema<\/code> in which we have a Table called Student. We can make this table with the following SQL command:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Make Student table<\/em><\/span><\/p>\n<pre class=\"brush:bash\">CREATE Table Student IF NOT EXISTS (\r\n  id INT PRIMARY KEY AUTO_INCREMENT,\r\n  name VARCHAR(100),\r\n  roll_no INT NOT NULL\r\n);\r\n<\/pre>\n<p>We could have integrated this SQL query to create a new Table in the application itself if we want because that way, we won&#8217;t have to be depend on an individual SQL query to create our table when the code for the app itself could have created that table for us when the app was run. We can now add the following line of code to make sure that the connection is made to MySQL:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Making connection to MySQL server<\/em><\/span><\/p>\n<pre class=\"brush:bash\">connection = mysql.connect()\r\n<\/pre>\n<p>Please note that as this is just a demonstration lesson and not a production-grade application, we do not manage the database connection pooling here. In any application, once we focus on database connection pooling for productionisation of our application, it makes a huge difference because <strong>database connections are expensive<\/strong> to make and keep.<\/p>\n<h2 id=\"insert\">5. Insert new records<\/h2>\n<p>Once the connection is made, we can define a simple function which will insert the provided JSON data into the Student table. Here is the definition for the said function:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Insert new student function<\/em><\/span><\/p>\n<pre class=\"brush:bash\">insert_sql_query = \"INSERT INTO Student (`name`, `roll_no`) VALUES (%s, %s)\"\r\ndata = (new_student['name'], new_student['roll_no'])\r\ncursor = connection.cursor()\r\ncursor.execute(insert_sql_query, data)\r\n<\/pre>\n<p>Note that we just pass a simple SQL query. Even though we have defined the query above, the data will still not be inserted into the database, at least not yet. For this to happen, we must commit the transaction:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Committing a Transaction<\/em><\/span><\/p>\n<pre class=\"brush:bash\">connection.commit()\r\n<\/pre>\n<p>If you do not commit the transaction, the insert query will be rolled-back once the current session with the application is terminated. This is not something we need in our apps, so always commit the transactions you do.<\/p>\n<p>Let&#8217;s look at the POST Postman call we made to insert a new Student table:<\/p>\n<p><figure id=\"attachment_61423\" aria-describedby=\"caption-attachment-61423\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/insert-new-student-mysql.png\"><img decoding=\"async\" class=\"wp-image-61423 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/insert-new-student-mysql.png\" alt=\"MySQL Python - Insert new Student in MySQL\" width=\"820\" height=\"727\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/insert-new-student-mysql.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/insert-new-student-mysql-300x266.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/insert-new-student-mysql-768x681.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61423\" class=\"wp-caption-text\">Insert new Student in MySQL<\/figcaption><\/figure><\/p>\n<p>Now that we have a Student in our Table, we can move to other operations as well. For now, the complete function for <strong>insert<\/strong> functionality will look like:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>INSERT function<\/em><\/span><\/p>\n<pre class=\"brush:bash\">@app.route('\/student', methods=['POST'])\r\ndef insert_student():\r\n    new_student = request.get_json()\r\n    insert_sql_query = \"INSERT INTO Student (`name`, `roll_no`) VALUES (%s, %s)\"\r\n    data = (new_student['name'], new_student['roll_no'])\r\n    cursor = connection.cursor()\r\n    cursor.execute(insert_sql_query, data)\r\n    connection.commit()\r\n    return 'Student inserted with name: %s' % new_student['name']\r\n<\/pre>\n<p>We see that we are not setting the update and where values in the query directly because that is a security vulnerability we can avoid just by using Prepared statements. That is what we have done by defining a Tuple <code>data<\/code> which contains the actual values to be inserted. This is really important because, without this, the application can easily become a victim of <strong>SQL Injection<\/strong> attacks which must be avoided. If you are interested in exploring more, read\u00a0<a href=\"https:\/\/www.javacodegeeks.com\/2013\/12\/using-sql-injection-vulnerabilities-to-dump-your-database.html\" target=\"_blank\" rel=\"noopener\">Using SQL Injection Vulnerabilities to Dump Your Database<\/a>\u00a0for much more information and knowledge.<\/p>\n<p>We were able to insert data into our table using the Python Flask application. Please note that we can perform any database operation just like what we did above, may it be a Delete or Update operation. We can even use a UI Framework which binds to Flask which will help us to view all the data we have in our table into a nice UI view but for now, we will stick to an API based calls to operate on the queries we define.<\/p>\n<h2 id=\"select\">6. Display data from MySQL<\/h2>\n<p>Now that we have some data in our table, we can display the same by making a GET call on the app route we defined to get a student with a specific ID. Please note that IDs are auto-generated because of the fact we defined them like that when we ran an SQL query to construct the Student table.<\/p>\n<p>To show data from the database, we need to run a SELECT query for the data. Let us define the SELECT statement here:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>SELECT statement<\/em><\/span><\/p>\n<pre class=\"brush:bash\">cursor = connection.cursor()\r\ncursor.execute(\"SELECT * from Student WHERE id='\" + student_id + \"'\")\r\n<\/pre>\n<p>Note that we have defined a cursor object to go over the data we fetch from the above query. We can run this command now:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Run SELECT command<\/em><\/span><\/p>\n<pre class=\"brush:bash\">student_data = cursor.fetchone()\r\nif student_data is None:\r\n  return \"Wrong ID passed.\"\r\nelse:\r\n  return jsonify(student_data)\r\n<\/pre>\n<p>When we run the above command, we will get back the following JSON containing the data for the Student for which we passed the ID to the application route:<\/p>\n<p><figure id=\"attachment_61415\" aria-describedby=\"caption-attachment-61415\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-student-mysql.png\"><img decoding=\"async\" class=\"wp-image-61415 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-student-mysql.png\" alt=\"MySQL Python - Get Student data from MySQL\" width=\"820\" height=\"477\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-student-mysql.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-student-mysql-300x175.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-student-mysql-768x447.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61415\" class=\"wp-caption-text\">Get Student data from MySQL<\/figcaption><\/figure><\/p>\n<p>Note that we do not exactly get the JSON we expected in the form of key-value pair. Instead, we get back a cursor representation of the data which is in the form of an array itself where each item represents the value for a column and is ordered in manner we defined them in the table. The complete function for insert functionality will look like:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Complete SELECT command<\/em><\/span><\/p>\n<pre class=\"brush:bash\">@app.route('\/student\/&lt;student_id&gt;', methods=['GET'])\r\ndef get_student(student_id):\r\n    cursor = connection.cursor()\r\n    cursor.execute(\"SELECT * from Student WHERE id='\" + student_id + \"'\")\r\n    student_data = cursor.fetchone()\r\n    if student_data is None:\r\n     return \"Wrong ID passed.\"\r\n    else:\r\n     return jsonify(student_data)\r\n<\/pre>\n<p>If you want to get back a more sophisticated JSON based result which correct key-value pair, feel free to explore some JSON based libraries for the same and mention them in the comments below.<\/p>\n<h2 id=\"update\">7. Update existing records in MySQL<\/h2>\n<p>Now that there is some data present for our Student in the database, we can move to update an existing tuple in our table Student. This can be done by passing in the JSON containing data related to our Student. Let us define the function and query which will perform this, note that we also include the commit call to the connection object as well so that this query is executed and the data for the corresponding Student is updated:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Update a Student function<\/em><\/span><\/p>\n<pre class=\"brush:bash\">update_sql_query = \"Update Student SET name=%s, roll_no=%s WHERE id=%s\"\r\ndata = (updated_student['name'], updated_student['roll_no'], updated_student['id'])\r\n<\/pre>\n<p>Let us execute the udpate call in Postman with the following data:<\/p>\n<p><figure id=\"attachment_61562\" aria-describedby=\"caption-attachment-61562\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/update-student-mysql.png\"><img decoding=\"async\" class=\"wp-image-61562 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/update-student-mysql.png\" alt=\"MySQL Python - Update student info\" width=\"820\" height=\"500\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/update-student-mysql.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/update-student-mysql-300x183.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/update-student-mysql-768x468.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61562\" class=\"wp-caption-text\">Update student info<\/figcaption><\/figure><\/p>\n<p>Now if we call the GET method for the same student, we will get the following output:<\/p>\n<p><figure id=\"attachment_61418\" aria-describedby=\"caption-attachment-61418\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-updated-student-mysql.png\"><img decoding=\"async\" class=\"wp-image-61418 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-updated-student-mysql.png\" alt=\"MySQL Python - Get update student info from MySQL\" width=\"820\" height=\"518\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-updated-student-mysql.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-updated-student-mysql-300x190.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/get-updated-student-mysql-768x485.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61418\" class=\"wp-caption-text\">Get update student info from MySQL<\/figcaption><\/figure><\/p>\n<p>The complete function for insert functionality will look like:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Complete UPDATE command<\/em><\/span><\/p>\n<pre class=\"brush:bash\">@app.route('\/student', methods=['PUT'])\r\ndef update_student():\r\n    updated_student = request.get_json()\r\n    update_sql_query = \"Update Student SET name=%s, roll_no=%s WHERE id=%s\"\r\n    data = (updated_student['name'], updated_student['roll_no'], updated_student['id'])\r\n    cursor = connection.cursor()\r\n    cursor.execute(update_sql_query, data)\r\n    connection.commit()\r\n    return 'Student updated with ID: %s' % updated_student['id']\r\n<\/pre>\n<h2 id=\"delete\">8. Delete data in MySQL<\/h2>\n<p>One of the most important operation in SQL is DELETE operation. This is what we will be demonstrating in this section by executing one of the DELETE statement in the application. We have defined a simple DELETE method which accepts an ID of a single student and now, we will define the helper method which will delete the student with the provided ID:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Delete a Student<\/em><\/span><\/p>\n<pre class=\"brush:bash\"><\/pre>\n<p>Once we run the delete method call, we will see the following output:<\/p>\n<p><figure id=\"attachment_61419\" aria-describedby=\"caption-attachment-61419\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/delete-student-mysql.png\"><img decoding=\"async\" class=\"wp-image-61419 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/delete-student-mysql.png\" alt=\"MySQL Python - Delete Student from MySQL\" width=\"820\" height=\"471\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/delete-student-mysql.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/delete-student-mysql-300x172.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/delete-student-mysql-768x441.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61419\" class=\"wp-caption-text\">Delete Student from MySQL<\/figcaption><\/figure><\/p>\n<p>If we try to GET that same student again, we will see an error in Postman:<\/p>\n<p><figure id=\"attachment_61420\" aria-describedby=\"caption-attachment-61420\" style=\"width: 820px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/deleted-student-error-mysql.png\"><img decoding=\"async\" class=\"wp-image-61420 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/deleted-student-error-mysql.png\" alt=\"MySQL Python - Error when getting deleted Student from MySQL\" width=\"820\" height=\"495\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/deleted-student-error-mysql.png 820w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/deleted-student-error-mysql-300x181.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/deleted-student-error-mysql-768x464.png 768w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><figcaption id=\"caption-attachment-61420\" class=\"wp-caption-text\">Error when getting deleted Student from MySQL<\/figcaption><\/figure><\/p>\n<p>At this stage, our application is complete. Here is the final code for our only Python file we made for the complete working project:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Complete Code<\/em><\/span><\/p>\n<pre class=\"brush:bash\">from flask import Flask, request, jsonify\r\nfrom flaskext.mysql import MySQL\r\n\r\napp = Flask(__name__)\r\nmysql = MySQL()\r\n\r\napp.config['MYSQL_DATABASE_USER'] = 'root'\r\napp.config['MYSQL_DATABASE_PASSWORD'] = 'root'\r\napp.config['MYSQL_DATABASE_DB'] = 'jcg_schema'\r\napp.config['MYSQL_DATABASE_HOST'] = 'localhost'\r\nmysql.init_app(app)\r\nconnection = mysql.connect()\r\n\r\n@app.route('\/hello')\r\ndef hello_world():\r\n    return 'Hello, World!'\r\n\r\n@app.route('\/student', methods=['POST'])\r\ndef insert_student():\r\n    new_student = request.get_json()\r\n    insert_sql_query = \"INSERT INTO Student (`name`, `roll_no`) VALUES (%s, %s)\"\r\n    data = (new_student['name'], new_student['roll_no'])\r\n    cursor = connection.cursor()\r\n    cursor.execute(insert_sql_query, data)\r\n    connection.commit()\r\n    return 'Student inserted with name: %s' % new_student['name']\r\n\r\n@app.route('\/student\/&lt;student_id&gt;', methods=['GET'])\r\ndef get_student(student_id):\r\n    cursor = connection.cursor()\r\n    cursor.execute(\"SELECT * from Student WHERE id='\" + student_id + \"'\")\r\n    student_data = cursor.fetchone()\r\n    if student_data is None:\r\n     return \"Wrong ID passed.\"\r\n    else:\r\n     return jsonify(student_data)\r\n\r\n@app.route('\/student', methods=['PUT'])\r\ndef update_student():\r\n    updated_student = request.get_json()\r\n    update_sql_query = \"Update Student SET name=%s, roll_no=%s WHERE id=%s\"\r\n    data = (updated_student['name'], updated_student['roll_no'], updated_student['id'])\r\n    cursor = connection.cursor()\r\n    cursor.execute(update_sql_query, data)\r\n    connection.commit()\r\n    return 'Student updated with ID: %s' % updated_student['id']\r\n\r\n@app.route('\/student\/&lt;int:student_id&gt;', methods=['DELETE'])\r\ndef delete_student(student_id):\r\n    cursor = connection.cursor()\r\n    cursor.execute(\"DELETE FROM Student WHERE id=%s\", (student_id,))\r\n    connection.commit()\r\n    return 'Deleted Student data with ID: %s' % student_id\r\n<\/pre>\n<p>The above code can be run by first setting up a virtual environment and installing all the packages we talked about in the beginning of this lesson. I highly recommend you to look at <a href=\"http:\/\/flask-sqlalchemy.pocoo.org\/2.3\/quickstart\/\" target=\"_blank\" rel=\"noopener\">SQLAlachemy<\/a> which provides us a way to use Model ORM mapping which is mostly used in a production-ready application.<\/p>\n<h2 id=\"conclusion\">9. Conclusion<\/h2>\n<p>In this lesson, we looked at a very simple yet effective example of setting up a connection to a MySQL server from a Python application. We started with important techniques to establish and close the connection with the server along with database connection pooling techniques which are very important to have in any production-grade application based on any framework and not just Python. We encourage you to study more about Python and MySQL to run more complex examples which involve Prepared statements and result sets. One of the more important things which can be done inside a Python application is calling stored procedures,\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/sql\/mysql-triggers-tutorial\/\" target=\"_blank\" rel=\"noopener\">define triggers<\/a>\u00a0and\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/sql\/mysql-full-text-search-tutorial\/\" target=\"_blank\" rel=\"noopener\">adding full-text search capabilities<\/a>\u00a0to the application.<\/p>\n<p>MySQL Stored procedures are a very powerful tool with very high performance (usually) but the bad thing is that they can grow very large very easily and quickly once your application starts to grow. The main database consistency should be managed at the application level wherever and whenever possible. Still, MySQL Stored procedures make a very good companion if you are a database administrator and just want to make things consistent without relying on too much code because after all, the performance of Stored procedures is very high because database operations are very fast on the database server itself. Don\u2019t forget to check the examples of using Java with\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/enterprise-java\/sql-enterprise-java\/jdbc-best-practices-tutorial\/\" target=\"_blank\" rel=\"noopener\">SQL: JDBC Best Practices Tutorial<\/a>\u00a0and\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/sql\/resultset-sql\/java-jdbc-resultset-example\/\" target=\"_blank\" rel=\"noopener\">Java JDBC ResultSet Example<\/a>. These lessons explain how to effectively use SQL commands with Java &amp;\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/enterprise-java\/sql-enterprise-java\/jdbc-driver-types-example\/\" target=\"_blank\" rel=\"noopener\">JDBC drivers<\/a>. There are many more examples present on MySQL which can be studied for a deeper understanding of the database. Finally, do read about some of\u00a0<a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/sql\/mysql-most-popular-functions-tutorial\/\" target=\"_blank\" rel=\"noopener\">the most popular MySQL functions<\/a>\u00a0which you will find interesting and useful in your daily life when you deal with MySQL. There are many more examples present on MySQL which can be studied for a deeper understanding of the database.<\/p>\n<p>Finally, even though we made use of a Python framework known as Flask to make this application, you could have used any other framework with very similar working. Other frameworks can be Django etc. or you could have made a standalone Python application as well which is not bound to any framework. That is a decision which can be made easily for a learning. Please feel free to share your feedback for the lesson in the comments below.<\/p>\n<h2 id=\"download\">10. Download the Source Code<\/h2>\n<p>This was an example of MySQL with Python programming language with Flask framework.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <strong><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/11\/PythonMySQLApp.zip\" target=\"_blank\" rel=\"noopener\">Python MySQL Example<\/a><\/strong><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. MySQL Python &#8211; Introduction In this post, we feature a comprehensive Tutorial on integrating MySQL in a simple Python application based on Flask and execute basic CRUD operations with the same.\u00a0MySQL\u00a0is one of the most used SQL databases in many of the world-enterprise applications due to its ability to manage transactions and the ACID-behaviour &hellip;<\/p>\n","protected":false},"author":154,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[1737,1716,1055],"class_list":["post-61343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql","tag-flask","tag-python","tag-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>MySQL Python Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Interested to learn more about MySQL? Then check out our detailed example on MySQL Python! MySQL is one of the most used SQL databases.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Python Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Interested to learn more about MySQL? Then check out our detailed example on MySQL Python! MySQL is one of the most used SQL databases.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-11-16T09:00:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Shubham Aggarwal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@sbmaggarwal\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shubham Aggarwal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/\"},\"author\":{\"name\":\"Shubham Aggarwal\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/da48da5ffe2c95ab19f7b2162a3f30b2\"},\"headline\":\"MySQL Python Example\",\"datePublished\":\"2018-11-16T09:00:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/\"},\"wordCount\":2678,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"flask\",\"python\",\"sql\"],\"articleSection\":[\"sql\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/\",\"name\":\"MySQL Python Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2018-11-16T09:00:36+00:00\",\"description\":\"Interested to learn more about MySQL? Then check out our detailed example on MySQL Python! MySQL is one of the most used SQL databases.\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"sql\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/sql\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"MySQL Python Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/da48da5ffe2c95ab19f7b2162a3f30b2\",\"name\":\"Shubham Aggarwal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/09\/Shubham-Aggarwal_avatar_1536328481-96x96.jpeg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/09\/Shubham-Aggarwal_avatar_1536328481-96x96.jpeg\",\"caption\":\"Shubham Aggarwal\"},\"description\":\"Shubham is a Java Backend and Data Analytics Engineer with more than 3 years of experience in building quality products with Spring Boot, MongoDB, Elasticsearch, MySQL, Docker, AWS, Git, PrestoDB tools and I have a deep knowledge and passion towards analytics, Micro-service based architecture, design patterns, antipatterns and software design thinking.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/sbmaggarwal\/\",\"https:\/\/x.com\/sbmaggarwal\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/shubham-aggarwal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL Python Example - Java Code Geeks","description":"Interested to learn more about MySQL? Then check out our detailed example on MySQL Python! MySQL is one of the most used SQL databases.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Python Example - Java Code Geeks","og_description":"Interested to learn more about MySQL? Then check out our detailed example on MySQL Python! MySQL is one of the most used SQL databases.","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-11-16T09:00:36+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Shubham Aggarwal","twitter_card":"summary_large_image","twitter_creator":"@sbmaggarwal","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Shubham Aggarwal","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/"},"author":{"name":"Shubham Aggarwal","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/da48da5ffe2c95ab19f7b2162a3f30b2"},"headline":"MySQL Python Example","datePublished":"2018-11-16T09:00:36+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/"},"wordCount":2678,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["flask","python","sql"],"articleSection":["sql"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/","name":"MySQL Python Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2018-11-16T09:00:36+00:00","description":"Interested to learn more about MySQL? Then check out our detailed example on MySQL Python! MySQL is one of the most used SQL databases.","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/sql\/mysql-python-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"sql","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/sql\/"},{"@type":"ListItem","position":5,"name":"MySQL Python Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/da48da5ffe2c95ab19f7b2162a3f30b2","name":"Shubham Aggarwal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/09\/Shubham-Aggarwal_avatar_1536328481-96x96.jpeg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2018\/09\/Shubham-Aggarwal_avatar_1536328481-96x96.jpeg","caption":"Shubham Aggarwal"},"description":"Shubham is a Java Backend and Data Analytics Engineer with more than 3 years of experience in building quality products with Spring Boot, MongoDB, Elasticsearch, MySQL, Docker, AWS, Git, PrestoDB tools and I have a deep knowledge and passion towards analytics, Micro-service based architecture, design patterns, antipatterns and software design thinking.","sameAs":["https:\/\/www.linkedin.com\/in\/sbmaggarwal\/","https:\/\/x.com\/sbmaggarwal"],"url":"https:\/\/examples.javacodegeeks.com\/author\/shubham-aggarwal\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/61343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/154"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=61343"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/61343\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=61343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=61343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=61343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}