Passing variables to the MYSQL Query from Command line -


i don't want burden people whole code, i'm going try parts answer question

i need add date range input python call

python query2.py -v --start_date='2013-08-12' --finish_date='2013-08-15' > output.txt 

but when try add concatenation string sql_query variable in query subroutine error:

    sql_query += """ having sm.created >= '2013-08-12' , sm.created <= '2013-0 8-15' """ unboundlocalerror: local variable 'sql_query' referenced before assignment killed signal 2. 

so im guessing cant add variable in manner,

sql_query += " having sm.created >= " + options.db_start + " , sm.created <= " + options.db_finish 

could tell me wrong?

thanks

import sys import os import time import optparse import getpass import subprocess contextlib import contextmanager import mysqldb   # todo: modify columns, course_id make sense sql_query = """ select au.email, sm.created, sce.created , sm.grade, sm.max_grade auth_user au, courseware_studentmodule sm,  student_courseenrollment sce sm.student_id = au.id , sm.student_id = sce.id , sm.course_id = 'mycourse' , sm.module_type = 'problem'  """  def query():     """      sql_query += " having sm.created >= " + options.db_start + " , sm.created <= " + 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 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() 

in query() function must use global sql_query first. what's going on here python assumes name assigned to, anywhere within function, local function unless explicitly told otherwise. if reading name, , name doesn't exist locally, try name in containing scopes (e.g. module's global scope).


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -