{"id":29,"date":"2023-11-26T19:27:38","date_gmt":"2023-11-27T00:27:38","guid":{"rendered":"https:\/\/coding101.xyz\/?p=29"},"modified":"2023-12-02T20:02:12","modified_gmt":"2023-12-03T01:02:12","slug":"python-and-mysql","status":"publish","type":"post","link":"https:\/\/coding101.xyz\/?p=29","title":{"rendered":"Python and MySQL"},"content":{"rendered":"\n<p>This article shows how to connect to mysql from python. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>The steps are as follows<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>create your venv<\/li>\n\n\n\n<li>install the driver package<\/li>\n\n\n\n<li>confirm the connectivity parameters<\/li>\n\n\n\n<li>proceed<\/li>\n<\/ul>\n\n\n\n<p>The best recommendation is to use a connection pool. The driver uses connection pooling, you just need to use it. This will allow avoiding keeping creating a new connection and destroying it, while you can use connections maintained alive as part of a pool. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install Driver<\/h2>\n\n\n\n<p>The driver is installed with the following command<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install mysql-connector-python<\/code><\/pre>\n\n\n\n<p>There are other drivers, however this one seems to be easiest to install<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Confirm Connectivity Parameters<\/h2>\n\n\n\n<p>So we use a database installed locally (localhost) on mariadb server lisening to standard port 3306, a database called test, user test and password &#8216;test1&#8217;. The table we created is tinf with fields id and comments, both varchar; <\/p>\n\n\n\n<p>OK so now we want to connect to it. This is how we do it. The config below contains all the parameters, although we could have omitted the host and the port, since they are the standard ones. <\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">mysql.connector<\/span>\n\ndbconfig <span style=\"color: #333333\">=<\/span> {\n    <span style=\"background-color: #fff0f0\">&#39;database&#39;<\/span>: <span style=\"background-color: #fff0f0\">&#39;test&#39;<\/span>,\n    <span style=\"background-color: #fff0f0\">&#39;user&#39;<\/span>: <span style=\"background-color: #fff0f0\">&#39;test&#39;<\/span>,\n    <span style=\"background-color: #fff0f0\">&#39;password&#39;<\/span>: <span style=\"background-color: #fff0f0\">&#39;test1&#39;<\/span>,\n    <span style=\"background-color: #fff0f0\">&#39;host&#39;<\/span>: <span style=\"background-color: #fff0f0\">&#39;localhost&#39;<\/span>,\n    <span style=\"background-color: #fff0f0\">&#39;port&#39;<\/span>: <span style=\"color: #0000DD; font-weight: bold\">3306<\/span>\n}\n\npool <span style=\"color: #333333\">=<\/span> mysql<span style=\"color: #333333\">.<\/span>connector<span style=\"color: #333333\">.<\/span>pooling<span style=\"color: #333333\">.<\/span>MySQLConnectionPool(pool_name<span style=\"color: #333333\">=<\/span><span style=\"background-color: #fff0f0\">&#39;starter_pool&#39;<\/span>, pool_size<span style=\"color: #333333\">=<\/span><span style=\"color: #0000DD; font-weight: bold\">3<\/span>, <span style=\"color: #333333\">**<\/span>dbconfig)\ncnx <span style=\"color: #333333\">=<\/span> pool<span style=\"color: #333333\">.<\/span>get_connection()\ncursor <span style=\"color: #333333\">=<\/span> cnx<span style=\"color: #333333\">.<\/span>cursor()\ncursor<span style=\"color: #333333\">.<\/span>execute(<span style=\"background-color: #fff0f0\">&#39;select id, comments from tinf where id like %s&#39;<\/span>, (<span style=\"background-color: #fff0f0\">&#39;1i%&#39;<\/span>,))\n<span style=\"color: #008800; font-weight: bold\">for<\/span> row <span style=\"color: #000000; font-weight: bold\">in<\/span> cursor<span style=\"color: #333333\">.<\/span>fetchall():\n    <span style=\"color: #007020\">print<\/span>(row[<span style=\"color: #0000DD; font-weight: bold\">0<\/span>], row[<span style=\"color: #0000DD; font-weight: bold\">1<\/span>])\n\ncursor<span style=\"color: #333333\">.<\/span>close()\ncnx<span style=\"color: #333333\">.<\/span>close()\n<\/pre><\/div>\n\n\n\n<p>Note a couple of things<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>pool should be long lived<\/li>\n\n\n\n<li>each time you create a connection<\/li>\n\n\n\n<li>then you need a cursor. This is like a Statement at java, you post the commands against this object, not the connection<\/li>\n\n\n\n<li>At the end you close both the cursor and the connection, although I suspect that closing the connection (which in this case means releasing resources and returning the connection to the pool) will also close the cursor<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Transactions<\/h2>\n\n\n\n<p>Of course there are transactions in this context. If you need transactions, there are two steps<\/p>\n\n\n\n<p>Set autocommit to false to the connection, otherwise it is by default true<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"335\" height=\"95\" src=\"https:\/\/coding101.xyz\/wp-content\/uploads\/2023\/11\/image-2.png\" alt=\"\" class=\"wp-image-31\"\/><\/figure>\n\n\n\n<p>Then you can call either commit() method or rollback() also on the connection. Of course this is written in some context that takes exceptions into account etc. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to connect to mysql from python.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=29"}],"version-history":[{"count":3,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":98,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/29\/revisions\/98"}],"wp:attachment":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}