I have a django project that is connected to SQL Server using pyodbc.
I am able to connect and retrieve data.
But the problem is when i tried to filter the data on multiple fields using WHERE & LIKE the system crash crash and display the below error.
'tuple' object has no attribute 'format'
from django.shortcuts import render
import pyodbc
def connect(request):
conn = pyodbc.connect(
'Driver={ODBC Driver 17 for SQL Server};'
'Server=DESKTOP-LPD1575\\SQLEXPRESS;'
'Database=testDB;'
'UID=test;'
'PWD=test;'
)
query = 'n'
queryid = 3
cursor = conn.cursor()
c = cursor.execute('SELECT * FROM Artist where artistName like ? or id = ?',('%{0}%','{1}').format(query,queryid))
print(c)
return render (request,'connect.html',{"c":c})
<table align = "center">
<tr align="center">
<th>id</th>
<th>FolderNumber</th>
<th>Folderdate</th>
</tr>
{% for row in c %}
<tr align="center">
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
<td>{{ row.2 }}</td>
</tr>
{% endfor %}
</table>
If I understand correctly, you should be passing a tuple as the second parameter to cursor.execute()
, using just literal values:
c = cursor.execute('SELECT * FROM Artist WHERE artistName LIKE ? OR id = ?',
('%' + query + '%', queryid))