0

I want to write a wrapper script around the mysql cli client. So I can construct the mysql cli flags to be used based on some logic.

my question is:

How would I execute the mysql client so that the user can interact with it. (stdout,stdin forwarded etc)?

8
  • Perhaps a better title for your question is "How to wrap a process in a python script" Commented Mar 22, 2014 at 7:30
  • Ah I thought of that initially, but then I thought mentioning mysql would be more specific Commented Mar 22, 2014 at 7:35
  • Effective dupe. I'm pretty sure you just want to connect and run queries. Commented Mar 22, 2014 at 7:42
  • @U2EF1, no this is not a duplicate. He wants his users to be able to run queries interactively. Commented Mar 22, 2014 at 7:49
  • @wjimenez5271, I think what you will wind up with is a lot of people saying "uh duh, MySQLDB, or PyMySQL", with realizing your real problems is "forwarding" stin/stdout. Commented Mar 22, 2014 at 7:51

1 Answer 1

1

Just build your list of arguments and use the subprocess module to launch MySQL

import subprocess

args = ['-u', 'wjimenez5271', '-p']
subprocess.call('mysql', args)
print('done')

This will launch mysql -u wjimenez5271 -p, handing over control until the process terminates, then python will resume and print "done".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.