{"id":3856,"date":"2019-03-20T21:09:10","date_gmt":"2019-03-20T15:39:10","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=3856"},"modified":"2019-03-21T09:16:55","modified_gmt":"2019-03-21T03:46:55","slug":"tutorial-on-how-to-connect-python-with-mysql","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/tutorial-on-how-to-connect-python-with-mysql\/","title":{"rendered":"Tutorial on how to connect python with MySQldb"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Tutorial on how to connect python with MySQldb<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Date posted: 20\/03\/2019<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\">Introduction:<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">In this article helps you in  connect python with MySQldb and insert queries  in DB.&nbsp; This example is explained based on Linux environment in Centos 7.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/03\/mysql-python-connect.jpg\" alt=\"\" class=\"wp-image-3861\" width=\"280\" height=\"198\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Prerequisites:<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>For centOS and RHEL  based environment:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># sudo yum install python<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> 2. Pip installation is most important for installing python packages<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># curl -O https:\/\/bootstrap.pypa.io\/get-pip.py<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Confirm the installed PIP version so that we will proceed our work further:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># pip \u2013version<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"> <br><strong>Importing Modules and their Pip Installation:<\/strong> <\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">While Importing, if the packaging Modules of&nbsp; any one among the above missed, will arise&nbsp;<strong>&nbsp;Import error.<\/strong> To eradicate the error follow these step by step modules to import&nbsp; using pip Installation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Importing Mysqldb:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Yum install MYSQL-python<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/blog-4.png\"><img decoding=\"async\" width=\"470\" height=\"178\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/blog-4.png\" alt=\"\" class=\"wp-image-2989\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/blog-4.png 470w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/blog-4-300x114.png 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2. Importing mysql.connector:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install mysql-connector-python<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1.png\"><img decoding=\"async\" width=\"796\" height=\"169\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1.png\" alt=\"\" class=\"wp-image-2991\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1.png 796w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1-300x64.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1-768x163.png 768w\" sizes=\"(max-width: 796px) 100vw, 796px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Create a table and insert into it<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n\n# Connect to db and create a table called blog that each have a name field and insert a couple blogs\n\nimport MySQLdb as mysqldb\n\nconnection = mysqldb.connect('localhost', 'bloguser', 'blogpw', 'blogdb');\n\nwith connection:\n    cursor = connection.cursor()\n    cursor.execute(\"create table blogs(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(25))\")\n    cursor.execute(\"insert into blogs(name) values('author')\")\n cursor.execute(\"insert into blogs(name) values('editor')\")\n cursor.execute(\"insert into blogs(name) values('admin')\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>We can verify this on the MySQL side after running<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql&gt; select * from blogs;\n+----+-----------+\n| id | name      |\n+----+-----------+\n| 2 | author    |\n| 3 | editor    |\n| 4 | admin     |\n+----+-----------+\n3 rows in set (0.00 sec)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Select data from a table<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n\n# Connect to db and create a table called blogs that each have a name field and insert a couple blogs\n\nimport MySQLdb as mysqldb\n\nconnection = mysqldb.connect('localhost', 'bloguser', 'blogpw', 'blogdb');\n\nwith connection:\n    cursor = connection.cursor()\n    cursor.execute(\"select * from blogs\")\nblogs = cursor.fetchall()\n    for blog in blogs:\n        print(blog)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\n\nThat&#8217;s it! Hope it&#8217;s useful.\n\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for using&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/\">pheonix solutions<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You find this tutorial helpful? Share with your friends to keep it alive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial on how to connect python with MySQldb Date posted: 20\/03\/2019 Introduction: In this article helps you in connect python [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[586,179,585,292],"class_list":["post-3856","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-import-mysqldb","tag-mysql-2","tag-pip-installation","tag-python","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-10c","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=3856"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3856\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=3856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=3856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=3856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}