Home > Software engineering >  '[HY004] [Microsoft][ODBC Driver 17 for SQL Server]Invalid SQL data type (0) (SQLBindParameter)
'[HY004] [Microsoft][ODBC Driver 17 for SQL Server]Invalid SQL data type (0) (SQLBindParameter)

Time:06-29

Like on title Im having this problem on this line.

mycursor.execute("UPDATE slot SET plaka=?,girisTarih=?,girisSaat=?,musaitlik=?,  WHERE slotAd=?",plaka1,girisTarih,girisSaat,musaitlik,slotAd)

musaitlik = true/false(0-1) slotAd = A1

import cv2
from matplotlib import pyplot as plt
import easyocr
import PIL.Image
import PIL.ImageTk
import os
import pyodbc
import datetime
import numpy as np
import imutils
import random

conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=DESKTOP-L3245F\SQLEXPRESS;'
                      'Database=CarPark;'
                      'Trusted_Connection=yes;''autocommit=True')



def giris(plaka1):
    global plaka
    
    print("girdi")
    mycursor = conn.cursor()
    mycursor.execute("SELECT plaka from slot where plaka=?",(plaka1))
    kontrol=False
    for x in mycursor:
        if (x[0]==plaka1):
            kontrol=True
    if(kontrol):
        print("Bu Araç Otoparktadır")
    else:
        mycursor = conn.cursor()
        girisTarih = datetime.datetime.now().strftime("%d.%m.%Y")
        girisSaat = datetime.datetime.now().strftime("%H:%M")
        musaitlik = 0
        mycursor.execute("SELECT slotAd FROM slot WHERE musaitlik='1'")
        slotAd = mycursor.fetchone()
        mycursor.execute("UPDATE slot SET plaka=?,girisTarih=?,girisSaat=?,musaitlik=?,  WHERE slotAd=?",plaka1,girisTarih,girisSaat,musaitlik,slotAd)
        conn.commit()
        print(mycursor.rowcount, "Giriş Yapildi.")
        plaka=""

tantuni = "52 AT 533"
giris(tantuni)

CodePudding user response:

Try

slotAd = mycursor.fetchval() 

instead of

slotAd = mycursor.fetchone(). 

This solved my problem.

CodePudding user response:

Issue comes down to your attempt to parameterize a sequence/iterable object when passing the return of fetchone which does not return one scalar value but one sequence (i.e., row) of values. If only one column from query is returned then you have a sequence/iterable of one element.

Per the Python's DB-API Specification (PEP 249) of which pyodbc largely adheres to, fetchone is specified to return (emphasis added):

Fetch the next row of a query result set, returning a single sequence, or None when no more data is available.

Therefore, simply index the first item of sequence/iterable for specific first column value before parameterizing which expects only scalars:

mycursor.execute("SELECT slotAd FROM slot WHERE musaitlik='1'")
slotAd = mycursor.fetchone()[0]

mycursor.execute(
    "UPDATE slot SET plaka=?, girisTarih=?, girisSaat=?, musaitlik=? WHERE slotAd=?",
    plaka1, girisTarih, girisSaat, musaitlik, slotAd
)
conn.commit()

Per pyodbc docs, fetchval as suggested is characterized as a:

convenience method returns the first column of the first row if there are results, otherwise it returns None.

which may run either fetchall()[0][0] or fetchone()[0] under the hood.

  • Related