Python Forum
MySQLdb, problem with query with user-defined variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MySQLdb, problem with query with user-defined variables
#1
I have SQL query I try to run using MySQL-python.
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 row
I 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.
Reply


Messages In This Thread
MySQLdb, problem with query with user-defined variables - by buran - Feb-01-2017, 09:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  User-defined function to reset variables? Mark17 3 1,690 May-25-2022, 07:22 PM
Last Post: Gribouillis
  Multiple user defined plots with secondary axes using for loop maltp 1 1,491 Apr-30-2022, 10:19 AM
Last Post: maltp
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,522 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Setting permanent user variables in Windows coder420 2 1,448 Jan-04-2022, 10:42 AM
Last Post: Larz60+
  Problem Using SQL Placeholder In MySQL Query AdeS 11 6,218 Jul-31-2021, 12:19 AM
Last Post: Pedroski55
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,664 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Running scripts and location of saved interpreted user-defined classes and functions leodavinci1990 3 2,560 Aug-25-2020, 03:43 AM
Last Post: micseydel
  python library not defined in user defined function johnEmScott 2 3,909 May-30-2020, 04:14 AM
Last Post: DT2000
  Problem with user defined main menu function stefzeer 3 2,422 Mar-27-2020, 06:12 AM
Last Post: buran
  User defined functions inside other user defined functions WildP 1 1,964 Jan-29-2020, 04:57 PM
Last Post: Clunk_Head

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020