{"id":291,"date":"2016-04-27T04:01:35","date_gmt":"2016-04-27T08:01:35","guid":{"rendered":"http:\/\/jasonralph.org\/?p=291"},"modified":"2017-10-13T17:18:18","modified_gmt":"2017-10-13T21:18:18","slug":"python-mysql-connector-function-insert-data","status":"publish","type":"post","link":"https:\/\/jasonralph.org\/?p=291","title":{"rendered":"Python Mysql Connector"},"content":{"rendered":"<p>Thought I would try my hand at some SQL programming with Python, I was stuck using a Windows machine(BLAH) I wanted to setup a MySQL database and 1 table for testing. I am on a Windows machine so I installed WAMP which is a Windows Apache Mysql Php server. You can get the installer for Windows here:<br \/>\n<a href=\"http:\/\/www.wampserver.com\/en\/\">http:\/\/www.wampserver.com\/en\/<\/a><\/p>\n<p>You can get the python library \/ module here:<\/p>\n<p><a href=\"https:\/\/dev.mysql.com\/downloads\/connector\/python\/2.1.html\">Mysql Python<\/a><br \/>\nUsing PhpMyAdmin I setup a database and added the table.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"wp-content\/images\/mysql_show_databases.jpg\" alt=\"mysql\" width=\"220\" height=\"220\"><\/p>\n<p>As you can see I created a function that added data into my new database. I pass it 4 parameters, id-name-dept-salary, when you execute this script the database gets populated with the correct data. Pretty cool!!<\/p>\n<pre class=\"theme:solarized-dark lang:default decode:true \">#!\/usr\/bin\/python\r\nfrom mysql import connector\r\nimport optparse\r\n\r\nparser = optparse.OptionParser()\r\nparser.add_option('-i', action=\"store\", help=\"Enter Job ID#\", type=\"int\")\r\nparser.add_option('-n', action=\"store\", help=\"Enter Employee Name\", type=\"string\")\r\nparser.add_option('-d', action=\"store\", help=\"Enter Department\", type=\"string\")\r\nparser.add_option('-s', action=\"store\", help=\"Enter Salary\", type=\"int\")\r\noptions, args = parser.parse_args()\r\n\r\ndef databaseAdd(id,name,dept,salary):\r\n    con = connector.Connect(user='jason',\r\n                            password='password',\r\n                            database='jason',\r\n                            host='localhost')\r\n    c = con.cursor()\r\n    table_name = \"employee\"\r\n    insert = \"INSERT INTO \" + table_name + \" (id,name,dept,salary) \\\r\n    VALUES (%s, %s, %s, %s)\"\r\n    data_value = (id,name,dept,salary)\r\n    c.execute(insert, data_value)\r\n    con.commit()\r\n    c.close()\r\n    con.close()\r\n\r\ndatabaseAdd(options.i ,options.n,options.d,options.s)\r\n\r\n\r\n<\/pre>\n<p>Here is how you can see the help and call the command properly:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"wp-content\/images\/show_help_command.jpg\" alt=\"help command\" width=\"640\" height=\"440\"><\/p>\n<p>Here is what you can see when performing a select * from the table.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"wp-content\/images\/mysql_show_databases_2.jpg\" alt=\"database command\" width=\"640\" height=\"440\"><\/p>\n<p>Here is the awesome IDE pyCharm and the results from running the code.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"wp-content\/images\/pycharm.jpg\" alt=\"pycharm\" width=\"640\" height=\"440\"><br \/>\nHere is an example of how to query that database from the table.<\/p>\n<pre class=\"theme:solarized-dark lang:default decode:true \">from mysql import connector\r\nimport sys\r\nimport optparse\r\n\r\n\r\nparser = optparse.OptionParser()\r\nparser.add_option('--id', action=\"store\", help=\"Enter ID number\", type=\"int\")\r\noptions, args = parser.parse_args()\r\n\r\n\r\ntry:\r\n    cnx = connector.Connect(user='jason',\r\n                        password='password',\r\n                        database='jason',\r\n                        host='localhost')\r\nexcept connector.Error as err:\r\n    print \"Connection to Database Failed\"\r\n    print err\r\n    sys.exit(1)\r\n\r\n\r\ncursor = cnx.cursor()\r\nquery = \"SELECT * from employee WHERE id='%s'\" % options.id\r\ncursor.execute(query)\r\nfor row in cursor:\r\n    print row\r\n\r\n<\/pre>\n<p>Here is the output of the fetch command using ID as a parameter:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"wp-content\/images\/execute_program01.jpg\" alt=\"database command\" width=\"640\" height=\"440\"><\/p>\n<p>Took me a while to figure out the syntax, so hopefully this helps someone.<br \/>\nJason<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thought I would try my hand at some SQL programming with Python, I was stuck using a Windows machine(BLAH) I wanted to setup a MySQL database and 1 table for testing. I am on a Windows machine so I installed WAMP which is a Windows Apache Mysql Php server. You can get the installer for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[48,47,49,24,33],"class_list":["post-291","post","type-post","status-publish","format-standard","hentry","category-python","tag-mysql","tag-mysql-connector","tag-powershell","tag-python-2","tag-windows"],"_links":{"self":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=291"}],"version-history":[{"count":26,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/291\/revisions"}],"predecessor-version":[{"id":580,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/291\/revisions\/580"}],"wp:attachment":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}