microsoft/mssql-django

[QUESTION] How to use executemany with raw query?

INNORAON opened this issue · 3 comments

Question
I wonder how to use executemany with raw query?

I'm using mssql-django 1.3, Python 3.12, Django 4.2 version with SQLServer 2022 Enterprise.
Django model did not support composit PK, so I wrote all query on raw query using connection.

Than, when I write insert query include " Like 'a%' " and I use executemany with raw query, I have been experienced some problems.

For example
from django.db import connection

query_text = "UPDATE mytable SET table_name = %s, table_type = %s WHERE table_id = %s;"
params = [('a', 'b', 1), ('c', 'd', 2), ('e', 'f', 3), ('g', 'h', 4)]

with connection.cursor() as cursor:
cursor.executemany(query_text, params)

At this code, Error Occured! I didn't understand why occured errors.
TypeError: not enough arguments for format string

but when I change this query to using execute, this error didn't occured...

How to use executemany with connection raw qeury?

Hi @INNORAON, can you confirm that you're using mssql-django 1.3?

Hi @mShan0 .
yes, mssql-django version is 1.3, pyodbc version is 5.0.1
Thanks :)

Hi @INNORAON, can you confirm that you're using mssql-django 1.3?

Is it possible using executemany with connection raw query in mssql-django version 1.3?

When I update mssql-django version 1.4.2, I had another issue when using % in query text, %% was seem to recognized arguments in connection raw query.

So, I still use mssql-django version 1.3