{"id":7052,"date":"2020-08-03T18:30:40","date_gmt":"2020-08-03T13:00:40","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=7052"},"modified":"2023-12-22T19:05:22","modified_gmt":"2023-12-22T13:35:22","slug":"how-to-connect-mysql-using-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/how-to-connect-mysql-using-python\/","title":{"rendered":"How to Connect MySQL using Python"},"content":{"rendered":"<p>To connect MySQL using Python, follow the steps below:<\/p>\n<h2>Step 1: Install Python MySQL Connector<\/h2>\n<p>In order to connect to MySQL using Python, we need to first install the required driver. In this tutorial,we are going to use <strong>mysql-connector-python<\/strong> package.<\/p>\n<p>To install the package, run the following command.<\/p>\n<p><code><br \/>\npip install mysql-connector-python<br \/>\n<\/code><\/p>\n<h2>Step 2: Installing MySQL and creating Database<\/h2>\n<p>This is an optional step. If you already have MySQL installed, skip to the next step. Follow this tutorial to install <a href=\"https:\/\/codeforgeek.com\/how-to-install-mysql-on-macos\/\" rel=\"noopener noreferrer\" target=\"_blank\">MySQL in the Mac<\/a> operating system.<\/p>\n<p>Once MySQL is installed, run the following command to connect to MySQL.<\/p>\n<p><code><br \/>\nmysql -u root -p<br \/>\n<\/code><\/p>\n<p>You can use your own username in the place of <strong>root<\/strong>.<\/p>\n<p>Run this command in the MySQL shell to create a new database.<\/p>\n<p><code lang='sql'><br \/>\nCREATE DATABASE database_name;<br \/>\nuse database_name;<br \/>\n<\/code><\/p>\n<p>Replace database_name with your own database.<\/p>\n<p>Run this command to create a new table.<\/p>\n<p><code lang='sql'><br \/>\nCREATE TABLE users (name VARCHAR(255), address VARCHAR(255));<br \/>\n<\/code><\/p>\n<p>Add some data to the table.<\/p>\n<p><code lang='sql'><br \/>\nINSERT INTO users(name,address) VALUES ('Shahid', 'Mumbai');<br \/>\n<\/code><\/p>\n<h2>Step 3: Connecting MySQL using Python<\/h2>\n<p>Copy-paste the code shown below to your editor and save it as <strong>app.py<\/strong>.<\/p>\n<p><code lang='python'><br \/>\nimport mysql.connector<\/p>\n<p>mydb = mysql.connector.connect(<br \/>\n  host=\"localhost\",<br \/>\n  user=\"root\",<br \/>\n  password=\"\",<br \/>\n  database=\"database_name\"<br \/>\n)<\/p>\n<p>cursor = mydb.cursor()<\/p>\n<p>cursor.execute(\"SELECT * from users\")<\/p>\n<p>result = cursor.fetchall()<\/p>\n<p>for row in result:<br \/>\n  print(row)<br \/>\n<\/code><\/p>\n<p>Replace the database_name with your own database name.<\/p>\n<p>Run the program using the following command.<\/p>\n<p><code><br \/>\npython app.py<br \/>\n<\/code><\/p>\n<p>You should be getting the output as data from your MySQL table.<\/p>\n<blockquote><p>\nThis tutorial is a part of the free course \u2013 learn python from scratch. Enroll in this free course to learn Python from scratch.\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>To connect MySQL using Python, follow the steps below: Step 1: Install Python MySQL Connector In order to connect to MySQL using Python, we need to first install the required driver. In this tutorial,we are going to use mysql-connector-python package. To install the package, run the following command. pip install mysql-connector-python Step 2: Installing MySQL [&hellip;]<\/p>\n","protected":false},"author":69,"featured_media":7064,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134,18],"tags":[],"class_list":["post-7052","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-tutorial"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/Connect-mysql-using-python.jpg",1500,750,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/Connect-mysql-using-python-150x150.jpg",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/Connect-mysql-using-python-300x150.jpg",300,150,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/Connect-mysql-using-python-768x384.jpg",768,384,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/Connect-mysql-using-python-1024x512.jpg",1024,512,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/Connect-mysql-using-python.jpg",1500,750,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/Connect-mysql-using-python.jpg",1500,750,false]},"uagb_author_info":{"display_name":"Pankaj Kumar","author_link":"https:\/\/codeforgeek.com\/author\/pankaj\/"},"uagb_comment_info":0,"uagb_excerpt":"To connect MySQL using Python, follow the steps below: Step 1: Install Python MySQL Connector In order to connect to MySQL using Python, we need to first install the required driver. In this tutorial,we are going to use mysql-connector-python package. To install the package, run the following command. pip install mysql-connector-python Step 2: Installing MySQL&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/7052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/69"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=7052"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/7052\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/7064"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=7052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=7052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=7052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}