[英]'[HY004] [Microsoft][ODBC Driver 17 for SQL Server]Invalid SQL data type (0) (SQLBindParameter)' Issue
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 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)
Try尝试
slotAd = mycursor.fetchval()
instead of代替
slotAd = mycursor.fetchone().
This solved my problem.这解决了我的问题。
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 (ie, row) of values.问题归结为您在传递
fetchone
的返回值时尝试参数化序列/可迭代对象,该返回值不返回一个标量值,而是一个值序列(即行)。 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):根据
pyodbc
主要遵循的 Python 的 DB-API 规范 (PEP 249),指定fetchone
返回(强调添加):
Fetch the next row of a query result set, returning a single sequence , or None when no more data is available.
获取查询结果集的下一行,返回单个序列,或者当没有更多数据可用时返回 None。
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:根据 pyodbc 文档,建议的
fetchval
具有以下特征:
convenience method returns the first column of the first row if there are results, otherwise it returns None.
便捷方法如果有结果则返回第一行的第一列,否则返回 None。
which may run either fetchall()[0][0]
or fetchone()[0]
under the hood.它可以在后台运行
fetchall()[0][0]
或fetchone()[0]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.