{"id":342,"date":"2017-07-26T11:50:47","date_gmt":"2017-07-26T09:50:47","guid":{"rendered":"http:\/\/tomdus.de\/wp\/?p=342"},"modified":"2017-07-27T12:51:33","modified_gmt":"2017-07-27T10:51:33","slug":"connect-to-mysql-with-python","status":"publish","type":"post","link":"https:\/\/tomdus.de\/wp\/connect-to-mysql-with-python\/","title":{"rendered":"Connect to MySQL with python"},"content":{"rendered":"<p>The mysql libraries are not per see installed with python, thus:<\/p>\n<pre><code>sudo apt-get install python-mysql<\/code><\/pre>\n<p>Script:<\/p>\n<pre><code>\r\n#!\/usr\/bin\/python\r\nimport MySQLdb\r\n\r\ndb = MySQLdb.connect(host=\"localhost\", # your host, usually localhost\r\n    user=\"tomas\", # your username\r\n    passwd=\"tomas1234\", # your password\r\n    db=\"test\") # name of the data base\r\n\r\n# Create a cursor object that executes all required queries\r\ncur = db.cursor()\r\n\r\n# create sample table\r\ncur.execute(\"CREATE TABLE IF NOT EXISTS tomastest(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, data VARCHAR(100))\")\r\n\r\n# create sample data\r\ncur.execute(\"INSERT INTO tomastest(data) VALUES('Test data'); commit;\")\r\n\r\n# execute select sql\r\ncur.execute(\"SELECT * FROM tomastest\")\r\n\r\n# Get the number of rows in the resultset\r\nnumrows = cur.rowcount\r\nprint \"Rows count: \",numrows\r\nfor row in cur.fetchall():\r\n    print row[0], row[1]\r\ndb.close()\r\n<\/code><\/pre>\n<p>If not providede, thus create test mysql db (<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/creating-database.html\">sMySQL Docs<\/a>)<\/p>\n<pre><code>mysql&gt; CREATE DATABASE test;\r\nmysql&gt; use test<\/code><\/pre>\n<p>If not already exist create user (<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/create-user.html\">MySQL Docs<\/a>)<\/p>\n<pre><code>mysql&gt; CREATE USER 'tomas'@'localhost' IDENTIFIED BY 'tomas1234';\r\nmysql&gt; GRANT ALL ON test.* TO 'tomas'@'localhost';<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The mysql libraries are not per see installed with python, thus: sudo apt-get install python-mysql Script: #!\/usr\/bin\/python import MySQLdb db = MySQLdb.connect(host=&#8220;localhost&#8220;, # your host, usually localhost user=&#8220;tomas&#8220;, # your username passwd=&#8220;tomas1234&#8243;, # your password db=&#8220;test&#8220;) # name of the data base # Create a cursor object that executes all required queries cur = db.cursor() [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[19,17,18],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-mysql","tag-python"],"_links":{"self":[{"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/posts\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/comments?post=342"}],"version-history":[{"count":4,"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":349,"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/posts\/342\/revisions\/349"}],"wp:attachment":[{"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tomdus.de\/wp\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}