Indrajit Swain
Indrajit Swain

Reputation: 1483

Django Pyodbc Stored procedure not all arguments converted during string formatting 1- Sql server

I am unable to call STORED Procedure from Django. I am able to call use the STORED Procedure from normal python program. Sample working pythoncode

cursor = connect.cursor()
params=('IBM','2016')
cursor.execute("EXEC SP_Competitor_Extract ?, ? ",params)

This piece of code is working fine . But when i am trying to execute from Django it does not working .

def index(request):
    cursor = connection.cursor()
    try:
        params=["IBM", "2015"]
        cursor.execute("{call SP_Competitor_Extract (?,?)}",params)
        while cursor.nextset():
            try:
                results={'results':cursor.fetchall()}
                break
            except pyodbc.ProgrammingError:
                continue

This is giving me the error message not all arguments converted during string formatting

enter image description here

Upvotes: 2

Views: 976

Answers (2)

Indrajit Swain
Indrajit Swain

Reputation: 1483

Its working now in PYodbc there is no callproc . you can pass the argument directly . you dont need the place holder . In my case cursor.execute("{call SP_Competitor_Extract IBM,2016}") worked . i am getting all the values as variableand creating the SQL .cursor.execute(SQL)

Upvotes: 0

FlipperPA
FlipperPA

Reputation: 14311

Django's internal .cursor() method differs from pyodbc's method. Try this:

cursor.execute(
    "EXEC SP_Competitor_Extract %s, %s",
    [params]
)

For more information, the documentation covers it well here: https://docs.djangoproject.com/en/1.11/topics/db/sql/#executing-custom-sql-directly

The other option is to import connection from pyodbc and create a connection manually like you do in Python, with your Django DATABASES settings variables. Good luck!

Upvotes: 3

Related Questions