' [hy004] [Microsoft] [SQL Server的ODBC驱动程序17]无效SQL数据类型(0)(SQLBINDPARAMETER)'问题

发布于 2025-02-11 04:37:37 字数 1557 浏览 1 评论 0原文

就像标题IM在这条线上有这个问题一样。

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昔日梦未散 2025-02-18 04:37:37

尝试

slotAd = mycursor.fetchval() 

而不是

slotAd = mycursor.fetchone(). 

解决我的问题。

Try

slotAd = mycursor.fetchval() 

instead of

slotAd = mycursor.fetchone(). 

This solved my problem.

人疚 2025-02-18 04:37:37

问题归结为您在传递fetchone的返回时试图对序列/迭代对象进行参数化的尝试,该对象不返回一个 scalar 值,而是一个 sequence (即)值。如果仅返回查询中的一列,则您将具有一个元素的序列/迭代。

根据Python的DB-API规范(PEP 249),其中pyodbc在很大程度上坚持, fetchone 被指定为返回(添加了强调):

获取查询结果集的下一行,返回单个序列,或者在没有更多数据的情况下无。

因此,只需在参数化之前对特定的第一列值进行序列/迭代索引的第一项,它仅期望标量:

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 所建议的表征为:

便利方法如果有结果,则返回第一行的第一列,否则返回无。

可以在引擎盖下运行fetchall()[0] [0] [0] fetchone()[0] 。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文