Feb-01-2017, 09:09 PM
I have SQL query I try to run using MySQL-python.
My code
Error 2014: Commands out of sync; you can't run this command now.
I have no problem to run queries without user-defined variables. Tried to put the sql in stored procedure and call it two different ways (see commented lines), but get the same error.
Both sql query and the stored procedure work as expected in MySQL workbench.
Any ideas greatly appreciated. I'm reading different solutions re this error in the last couple of hours, but without success.
My code
import MySQLdb as mdb def mysql_query(sql, schema='my_schema', do_commit=False): server, username, password = get_db_credentials('mysql') try: with mdb.connect(host=server, user=username, passwd=password, db='my_schema', charset='utf8') as cursor: cursor.execute(sql) # cursor.callproc('irr', ()) if do_commit: conn.commit() return True else: res = cursor.fetchall() return (res,'') except mdb.Error, e: return([],'Error {} : {}'.format(e.args[0],e.args[1])) # sql = 'call my_schema.irr()' sql = ''' SET @runtot:=0.0; SET @runtot_bv:=0.0; SET @interest_exp:=0.0; SET @daily_interest_expense:=0.0; SELECT q1.value_date AS 'Value Date', q1.cash_flow AS 'Cash Flow', @runtot := @runtot + q1.cash_flow as 'Cash Flow Running Total', @daily_interest_expense := (@runtot_bv + q1.cash_flow)*(q1.daily_irr) AS 'Daily Interest Expense', (CASE WHEN DAYOFYEAR(q1.value_date) = 1 then @interest_exp := @daily_interest_expense ELSE @interest_exp := @interest_exp + @daily_interest_expense end) as 'Yearly Interest Expense', @runtot_bv := (@runtot_bv + q1.cash_flow)*(1+q1.daily_irr) as 'Balance Sheet Value' FROM cash_flow AS q1; ''' res = mysql_query(sql) for row in res: print rowI got MySQL error
Error 2014: Commands out of sync; you can't run this command now.
I have no problem to run queries without user-defined variables. Tried to put the sql in stored procedure and call it two different ways (see commented lines), but get the same error.
Both sql query and the stored procedure work as expected in MySQL workbench.
Any ideas greatly appreciated. I'm reading different solutions re this error in the last couple of hours, but without success.