views:

29

answers:

1

I'm writing a program that first queries with mySQL and then sorts that data. I want to be able to have a user type "python program_name.py mySQL_query" and have the program insert "mySQL_query" into the query at the beginning of the program. The issue I'm running into is the sys.argv command converts the input into a string, which mySQL then rejects. I've tried a few things to convert the sys.argv into a name instead of a string but they haven't been successful. Any ideas?

A: 

Your code needs to like something like this:

qb="SELECT DISTINCT q19_scan.array_orientation_equatorial, q19_scan.run_id, q19_scan.run_subid, q19_scan.patch_day_number, %s FROM q19_typeb NATURAL JOIN q19_scan NATURAL JOIN q19_timestream NATURAL JOIN q19_weather NATURAL JOIN q19_ces_usable WHERE " % sys.argv[2]

I have replaced sys.argv[2] in your query with %s, and then applied formatting operator on this string with second operand being sys.argv[2]. You can read more about python's formatting operator in documentation, or even use newer formatting functions:

thor