@@ -0,0 +1,1190 @@
+# -*- coding: utf-8 -*-%0A%22%22%22%0AReceive SQL query in stdin, send to configured database file, then return%0Athe query result rows.%0A%0AUsage:%0A    $ echo %22SELECT * FROM Trend LIMIT 10%22 %7C python -m lib.query.doQuery%0A%0A    $ python -m lib.query.doQuery %3C lib/query/sql/abc.sql %5C%0A        %3E var/reporting/abc.csv%0A%0ATODO%0A    Test with u'%5Cxed' character%0A%22%22%22%0Aimport sys%0A%0Afrom lib import database as db%0A%0A%0Adef quote(phrase):%0A    %22%22%22%0A    Remove double-quotes from a string and if there is a comma then returns%0A    value enclosed in double-quotes (ideal for outputting to CSV).%0A%0A    @param phrase: a string.%0A    %22%22%22%0A    # Remove double-quotes.%0A    phrase = phrase.replace('%22', %22'%22)%0A    # Add quotes if there is a comma.%0A    phrase = '%22%7B%7D%22'.format(phrase) if ',' in phrase else phrase%0A    return phrase%0A%0A%0Adef main(inputFile):%0A    %22%22%22%0A    Receive a SQL query as a string and execute then print results to stdout.%0A    %22%22%22%0A    for rowList in db.conn.queryAll(inputFile):%0A        # Any unicode characters will be lost (replaced with question marks)%0A        # by converting to str.%0A        rowStr = (quote(str(c)) for c in rowList)%0A        print ','.join(rowStr)%0A%0A%0Aif __name__ == '__main__':%0A    main(sys.stdin.read())%0A

Create doQuery.py