-1

So I am trying to create a script with a MySQL Query "show databases like 'login';"

I am able to use string substitution and use "login" but i am unable to get the single quotes around it.

Below is how I am trying to do it but i cant get the single quotes even if I escape it using "\".

  db = "'"+val+"'"
  print "DB...", db
  run.cmd("echo 'res = cur.execute(\"SHOW DATABASES like  %s;\")\n' >> /run.py" % (db))
user2921139
  • 1,669
  • 4
  • 17
  • 25

2 Answers2

0

Since your string is wrapped in double quotes you shouldn't need to escape the single quotes. Just add the val directly into the format, then add the single quotes to the string being formatted.

run.cmd("echo 'res = cur.execute(\"SHOW DATABASES like  '%s';\")\n' >> /run.py" % (val))

Also, is there any reason why you're appending a bash command into a python script?

asdf
  • 2,927
  • 2
  • 21
  • 42
  • This does not work. THe single quotes wont show up in run.py. Not sure why. Have u tried it? – user2921139 Mar 29 '17 at 17:22
  • @user2921139 what function are you pointing at with `run.cmd`? Is it built on top of `subprocess`? It could be an issue with that. A simple `print` replacing the `run.cmd` shows the quotes just fine. – asdf Mar 29 '17 at 18:40
  • @user2921139 I'm aware, but what is the `run.cmd` function calling/doing? I'd guess it's something wrong with the code there. Could you provide it? – asdf Mar 29 '17 at 18:53
  • Oh no, its just something internal to Python and my programming that I am doing. My way to making it run on a linux system. – user2921139 Mar 29 '17 at 22:16
0

SHOW DATABASES like '\'%s\'';\" -> this is what worked.

user2921139
  • 1,669
  • 4
  • 17
  • 25