0

I'm trying to get a Python script to work that downloads a file and then loads the data into a mariadb. My best option seems to be using subprocess.run, as I cannot update the Python version to a version that includes the new mariadb connector.

I've tried different options:

subprocess.run(["mysql", "-u" + mdb_usr, "-p" + mdb_pwd, "database", " < " + file.sql])
subprocess.run(["mysql", "-u" + mdb_usr, "-p" + mdb_pwd, "database < " + file.sql])
subprocess.run(["mysql -u" + mdb_usr + " -p" + mdb_pwd + "database < " + file.sql], shell = True)

But none of them seems to work. Without the < file.sql, I can get the script to work, but I can't seem to get the sql to execute. Can anyone point me in the right direction?

Joost
  • 82
  • 1
  • 9
  • You could try with `from subprocess import Popen` – wildplasser Jan 26 '22 at 11:31
  • If I read [this](https://stackoverflow.com/a/39187984/9066313), that wouldn't help – Joost Jan 26 '22 at 11:53
  • `p = Popen(cmd, close_fds=True, shell=True, stdin=PIPE, stdout=PIPE); p.stdin.write(smoke); answer = p.communicate()[0]; return answer` <<-- Try it. – wildplasser Jan 26 '22 at 11:59
  • Brilliant. Works (although no idea why or how), except that I need to now escape 1 character in the pwd string. Would there be a way to avoid that, so I don't have to remember for when I change the password? – Joost Jan 26 '22 at 12:41
  • It works by starting a sub-process (fork+exec), and feeding smoke to its stdin. It then sucks stdout until EOF. The subprocess then has ceased to exist, and the main process can continue. WRT the pwd (password?) : It is up to your code what to pass to the child process. – wildplasser Jan 26 '22 at 12:48

0 Answers0