Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing variables to the MYSQL Query from Command line
    primarykey
    data
    text
    <p>I don't want to burden people with my whole code, So i'm going to try to only get parts that could help answer my question</p> <p>I need to add a date range as an input to my python call</p> <pre><code>python query2.py -v --start_date='2013-08-12' --finish_date='2013-08-15' &gt; output.txt </code></pre> <p>But when I try to add this as a concatenation string to the SQL_QUERY variable in the query subroutine I get this error:</p> <pre><code> SQL_QUERY += """ HAVING sm.created &gt;= '2013-08-12' and sm.created &lt;= '2013-0 8-15' """ UnboundLocalError: local variable 'SQL_QUERY' referenced before assignment Killed by signal 2. </code></pre> <p>So im guessing I cant add the variable in that manner,</p> <pre><code>SQL_QUERY += " HAVING sm.created &gt;= " + options.db_start + " and sm.created &lt;= " + options.db_finish </code></pre> <p>Could someone tell me what wrong?</p> <p>Thanks</p> <pre><code>import sys import os import time import optparse import getpass import subprocess from contextlib import contextmanager import MySQLdb # TODO: modify this with columns, course_id that make sense for you SQL_QUERY = """ select au.email, sm.created, sce.created , sm.grade, sm.max_grade from auth_user au, courseware_studentmodule sm, student_courseenrollment sce where sm.student_id = au.id and sm.student_id = sce.id and sm.course_id = 'MyCourse' and sm.module_type = 'problem' """ def query(): """ SQL_QUERY += " HAVING sm.created &gt;= " + options.db_start + " and sm.created &lt;= " + options.db_finish log_info("query = %s" % SQL_QUERY) result = None conn = None try: conn = MySQLdb.connect(host='127.0.0.1', port=options.local_port, user=options.db_user, passwd=options.db_password, db=options.db_name); cur = conn.cursor() cur.execute(SQL_QUERY) result = cur.fetchall() except MySQLdb.Error as e: sys.stderr.write("Database error %d: %s\n" % (e.args[0],e.args[1])) if conn: conn.close() return result def parse_command_line(): usage = """usage: %prog [options] parser.add_option("--start_date", dest="db_start", help="Database host (default=\"%s\"" % DEFAULT_DB_HOST) parser.add_option("--finish_date", dest="db_finish", help="Database host (default=\"%s\"" % DEFAULT_DB_HOST) global options (options, args) = parser.parse_args() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload