{"id":900,"date":"2021-02-07T15:46:39","date_gmt":"2021-02-07T15:46:39","guid":{"rendered":"https:\/\/dft.wiki\/?p=900"},"modified":"2026-06-09T13:00:49","modified_gmt":"2026-06-09T17:00:49","slug":"using-python-with-mysql","status":"publish","type":"post","link":"https:\/\/dft.wiki\/?p=900","title":{"rendered":"Using Python with MySQL"},"content":{"rendered":"<p>MYSQL DATABASE WITH PYTHON<\/p>\n<p>Installing requirements:<\/p>\n<pre>sudo apt update\r\nsudo apt install python3-pip\r\nsudo pip3 install mysql-connector<\/pre>\n<p>Test importing MySQL in Python:<\/p>\n<pre>python3\r\n\r\n>> import mysql<\/pre>\n<p>If no error, it is working.<\/p>\n<p>Basic syntax to interact with MySQL:<\/p>\n<pre>import mysql.connector\r\n\r\ndb = mysql.connector.connect(host=\"localhost\", user=\"user\", passwd=\"password\", database=\"dbName\")\r\nmycursor = db.cursor()\r\nmycursor.execute(\"SHOW TABLES\")\r\n\r\nfor line in mycursor:\r\n  print(line)<\/pre>\n<p>Inserting data:<\/p>\n<pre>mycursor.execute(\"INSERT INTO %s (attrib1, attrib2) VALUES (%s, %s)\", (\"tableName\", \"data1\", \"data2\"))\r\ndb.commit()\r\n\r\nmycursor.execute(\"SELECT %s FROM %s\", (\"*\", \"tableName\"))\r\n\r\nfor line in mycursor:\r\n  print(line)<\/pre>\n<p>SEE ALSO<\/p>\n<p>SQL Cheat Sheet [<a href=\"https:\/\/dft.wiki\/?p=851\">Link<\/a>].<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MYSQL DATABASE WITH PYTHON Installing requirements: sudo apt update sudo apt install python3-pip sudo pip3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-900","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=900"}],"version-history":[{"count":7,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions"}],"predecessor-version":[{"id":5791,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions\/5791"}],"wp:attachment":[{"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dft.wiki\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}