{"id":111,"date":"2023-06-28T08:14:29","date_gmt":"2023-06-28T08:14:29","guid":{"rendered":"https:\/\/www.freebyte.fr\/?p=111"},"modified":"2023-06-28T11:36:31","modified_gmt":"2023-06-28T11:36:31","slug":"reminder-mysql","status":"publish","type":"post","link":"https:\/\/www.freebyte.fr\/?p=111","title":{"rendered":"Installation MariaDB"},"content":{"rendered":"<h2>Installation<\/h2>\n<pre>apt install mariadb-server mariadb-client\r\n\r\npuis configuration avec \r\n\r\nmysql_secure_installation\r\n\r\nOn veut maintenant cr\u00e9er une base de donn\u00e9e, et un utilisateur ayant tous les droits dessus \r\n\r\nmysql - u root\r\n&gt; CREATE DATABASE mabase;\r\n&gt; CREATE USER 'login'@localhost IDENTIFIED BY 'password';\r\n&gt; GRANT ALL PRIVILEGES ON mabase.* TO 'login'@localhost;\r\n&gt; FLUSH PRIVILEGES;<\/pre>\n<h2>MySql et Python<\/h2>\n<h4>Installer le connecteur python<\/h4>\n<pre class=\"lang:sh decode:true\">pip3 install mysql-connector<\/pre>\n<h4>Script python basique<\/h4>\n<pre class=\"lang:python decode:true \"># -*- coding: utf-8 -*-\r\n\r\nimport mysql.connector\r\n\r\nmydb = mysql.connector.connect(\r\n  host=\"localhost\",\r\n  user=\"utilisateur\",\r\n  passwd=\"motdepasse\",\r\n  database=\"books\"\r\n)\r\n'''\r\n La table 'livres' a 3 colonnes : Titre, Auteur et Langue\r\n qui sont tout les trois des VARCHAR(255)\r\n'''\r\n \r\nmycursor = mydb.cursor()\r\n\r\n# Insertion d'un enregistrement\r\nsql = \"INSERT INTO livres (Titre,Auteur,Langue) VALUES (%s,%s,%s)\"\r\nval = (\"L'Abominable\",\"Dan Simmons\",\"Fran\u00e7ais\")\r\nmycursor.execute(sql,val)\r\nmydb.commit()\r\n\r\n# Selection\r\nsql = \"SELECT * FROM livres\"\r\nmycursor.execute(sql)\r\nresultat = mycursor.fetchall()\r\n\r\nfor livre in resultat:\r\n\tprint(livre)\r\n\r\n# Selection avec param\u00e8tre\r\nsql = \"SELECT Titre FROM livres WHERE Titre = %s\"\r\nval = (\"L'Abominable\",) # Attention \u00e0 la virgule dans le tuple !)\r\nmycursor.execute(sql,val)\r\nresultat = mycursor.fetchall()\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Installation apt install mariadb-server mariadb-client puis configuration avec mysql_secure_installation On veut maintenant cr\u00e9er une base de donn\u00e9e, et un utilisateur ayant tous les droits dessus mysql &#8211; u root &gt; CREATE DATABASE mabase; &gt; CREATE USER &lsquo;login&rsquo;@localhost IDENTIFIED BY &lsquo;password&rsquo;; &gt; GRANT ALL PRIVILEGES ON mabase.* TO &lsquo;login&rsquo;@localhost; &gt; FLUSH PRIVILEGES; MySql et Python Installer [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":122,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3],"tags":[],"class_list":{"0":"post-111","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-info","8":"category-python","9":"czr-hentry"},"_links":{"self":[{"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=\/wp\/v2\/posts\/111"}],"collection":[{"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=111"}],"version-history":[{"count":7,"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=\/wp\/v2\/posts\/111\/revisions\/139"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=\/wp\/v2\/media\/122"}],"wp:attachment":[{"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=111"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=111"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freebyte.fr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}