{"id":900,"date":"2021-02-07T15:46:39","date_gmt":"2021-02-07T15:46:39","guid":{"rendered":"https:\/\/dft.wiki\/?p=900"},"modified":"2021-03-16T13:19:35","modified_gmt":"2021-03-16T13:19:35","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 to Python:<\/p>\n<pre>python3\r\n\r\n&gt;&gt; <strong>import mysql<\/strong><\/pre>\n<p>If no error, it is working.<\/p>\n<p>The basic syntax to interact with MySQL.<\/p>\n<pre><strong>import mysql.connector\r\n<\/strong>\r\ndb = mysql.<strong>connector.connect<\/strong>(host=\"localhost\",user=\"user\",passwd=\"password\",database=\"dbName\")\r\nmycursor = db<strong>.cursor<\/strong>()\r\nmycursor<strong>.execute<\/strong>(\"SHOW TABLES\")\r\n\r\n<strong>for<\/strong> line <strong>in<\/strong> mycursor:\r\n  print(line)<\/pre>\n<p>Inserting data:<\/p>\n<pre>mycursor<strong>.execute<\/strong>(\"INSERT INTO %s (attrib1, attrib2) VALUES (%s, %s)\", (\"tableName\",\"data1\",\"data2\"))\r\ndb<strong>.commit<\/strong>()\r\n\r\nmycursor<strong>.execute<\/strong>(\"SELECT %s FROM %s\", (\"*\",\"tableName\"))\r\n\r\n<strong>for<\/strong> line <strong>in<\/strong> 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":6,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions"}],"predecessor-version":[{"id":957,"href":"https:\/\/dft.wiki\/index.php?rest_route=\/wp\/v2\/posts\/900\/revisions\/957"}],"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}]}}