开发者

Python error: execute() first

When I try to run this view/function, I get the following error execute() first.

def homepage(request, parem1, parem2):
    # open sql connection
    cursor = connection.cursor()

    if parem1 == '开发者_StackOverflowp':

        if parem2 == 'all':   
            cursor.execute((" SELECT eventname, id, party, DATE_FORMAT(eventdate, GET_FORMAT(DATE,'EUR')) FROM mainapp_eventdetails ORDER BY eventdate DESC "))
        else:
            cursor.execute((" SELECT eventname, id, party, DATE_FORMAT(eventdate, GET_FORMAT(DATE,'EUR')) FROM mainapp_eventdetails WHERE party = '" + parem2 + "' ORDER BY eventdate DESC "))

    else:

        if parem2 == 'all':   
            cursor.execute((" SELECT eventname, id, party, DATE_FORMAT(eventdate, GET_FORMAT(DATE,'EUR')) FROM mainapp_eventdetails ORDER BY eventdate DESC "))
        elif parem2 == 'APR':
            cursor.execute((" SELECT eventname, id, party, DATE_FORMAT(eventdate, GET_FORMAT(DATE,'EUR')) FROM mainapp_eventdetails WHERE eventdate between '2011-4-1' AND '2011-5-1' ORDER BY eventdate DESC "))

    datalist = [memedetails for memedetails in cursor.fetchall()]

    # close sql connection
    cursor.close()
    connection.close()

    # group by eventdate
    datalistsorted = []

    # i dont like how this part depends on the sequence of columns retrieved in the above SQL. 
    for k, g in groupby(datalist, key=lambda x:x[3]):
        datalistsorted.append((k,[(i[0],i[1],i[2]) for i in g]))

    return render_to_response('base.html', {'eventlist': datalistsorted, 'parem2': parem2.upper()})

But if I were to change this line elif parem2 == 'APR': to else:, it works fine.

Why is this so and how can I get my elif to work? (There are more conditions below but I've omitted it for simplicity)


Presumably you've hit a case where parem1 is not 'p' and parem2 is not 'APR'. In this case, you haven't made any calls to execute.

Perhaps you should restrict the if/else to just defining the string to be executed, with a catch-all default, and do the execute call outside the if block.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜