{"id":16,"date":"2007-09-27T23:00:49","date_gmt":"2007-09-27T11:00:49","guid":{"rendered":"http:\/\/p-s.co.nz\/wordpress\/?p=16"},"modified":"2007-11-30T22:29:58","modified_gmt":"2007-11-30T10:29:58","slug":"running-mysql-scripts-sql-files-from-python","status":"publish","type":"post","link":"http:\/\/p-s.co.nz\/wordpress\/running-mysql-scripts-sql-files-from-python\/","title":{"rendered":"Running MySQL scripts (.sql files) from python"},"content":{"rendered":"<p>This should have been more obvious.  All I wanted was to run a simple script e.g. myscript.sql in MySQL from python in Windows.<\/p>\n<p>Some things worked fine from the DOS prompt but failed from within python (and RUN for that matter).<\/p>\n<p>Here is the answer in the form of a simple function (NB to get your indentation right !):<\/p>\n<pre>def run_sql_script(scriptname):\r\n    \"Run a script in MySQL\"\r\n    import subprocess\r\n    import time\r\n    #this next line is too long for this blog but you will need it on one line to run\r\n    args = \"\\\"C:\\\\Program Files\\\\MySQL\\\\MySQL Server 5.0\\\\bin\\\\mysql.exe\\\" \r\n        -h%s -u%s -p%s --database=databasename < C:\/Projects\/projectname\/3_scripts\/%s\" \r\n        % (DB_HOST, DB_USER, DB_PWD, scriptname)\r\n    #print args\r\n    child = subprocess.Popen(args=args, shell=True, executable=\"C:\\\\windows\\\\system32\\\\cmd.exe\")\r\n    #need to check whether finished or not every so often\r\n    i = True\r\n    elapsed = 0\r\n    while i == True:\r\n        time.sleep(10)\r\n        elapsed = elapsed + 10\r\n        if child.poll() == None:\r\n            elapsed_mins = float(elapsed)\/60\r\n            print \"%.2f minutes elapsed running %s\" % (elapsed_mins, scriptname)\r\n        else:\r\n            print \"Finished running script \" + scriptname\r\n            i = False\r\n<\/pre>\n<p>Key points: can't use call, must use full Popen and explicitly name the shell (and use it!).<\/p>\n<p>NB scripts can run for a long time e.g. 30 minutes so it is a good idea to make the function keep the user informed.<\/p>\n<p>If you want to kill it, open Task Manager and kill mysql.exe.<\/p>\n<p>In Windows there are apparently some horrible compromises to be made when doing some simple things.  See <a href=\"http:\/\/benjamin.smedbergs.us\/blog\/2006-11-09\/adventures-in-python-launching-subprocesses\/\">...adventures-in-python-launching-subprocesses\/<\/a><\/p>\n<p>There is an alternative approach but it doesn't seem to ever end the subprocess:<\/p>\n<p>#args = \"cmd \/k \\\"C:\\\\Program Files\\\\MySQL\\\\MySQL Server 5.0\\\\bin\\\\mysql.exe\\\" -h%s -u%s -p%s --database=databasename < C:\\\\Projects\\\\projectname\\\\test.sql\" % (DB_HOST, DB_USER, DB_PWD)\nprint args #http:\/\/www.realtechnews.com\/posts\/2777 re: the \/k !!!!!\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This should have been more obvious. All I wanted was to run a simple script e.g. myscript.sql in MySQL from python in Windows. Some things worked fine from the DOS prompt but failed from within python (and RUN for that &hellip; <a href=\"http:\/\/p-s.co.nz\/wordpress\/running-mysql-scripts-sql-files-from-python\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,3],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-mysql","category-python"],"_links":{"self":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":0,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/p-s.co.nz\/wordpress\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}