开发者

incorporating a sys.argv into a mySQL query in python

开发者 https://www.devze.com 2023-01-10 16:11 出处:网络
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\"

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?


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:

0

精彩评论

暂无评论...
验证码 换一张
取 消