简体   繁体   English

“[HY004] [Microsoft][ODBC Driver 17 for SQL Server]无效的 SQL 数据类型 (0) (SQLBindParameter)”问题

[英]'[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.

相关问题 Django [Microsoft][ODBC Driver 17 for SQL 服务器][SQL Server]无效的 object 名称 'MYSCHEMA.MyUnmanagedModel' - Django [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'MYSCHEMA.MyUnmanagedModel' pyodbc.ProgrammingError '42000',[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]找不到数据类型'TEXT'。 (104220) (SQLExecDirectW) - pyodbc.ProgrammingError '42000', [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot find data type 'TEXT'. (104220) (SQLExecDirectW) 'HY000', '[HY000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]无法打开登录请求的服务器“dominio”。 登录失败 - 'HY000', '[HY000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot open server "dominio" requested by the login. The login failed [Microsoft] [用于SQL Server的ODBC驱动程序17]登录超时已过期(0)(SQLDriverConnect)') - [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)') ('IM004',“[IM004] [Microsoft] [ODBC驱动程序管理器] SQL_HANDLE_ENV上的驱动程序SQLAllocHandle失败(0)(SQLDriverConnect)”)错误 - ('IM004', “[IM004] [Microsoft][ODBC Driver Manager] Driver's SQLAllocHandle on SQL_HANDLE_ENV failed (0) (SQLDriverConnect)”) error DatabaseError: ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0) (SQLExecDirectW)') - DatabaseError: ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Connection is busy with results for another hstmt (0) (SQLExecDirectW)') ('42000','[42000] [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]将数据类型nvarchar转换为int时出错。(8114)(SQLExecDirectW)') - ('42000', '[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Error converting data type nvarchar to int. (8114) (SQLExecDirectW)') (fast_executemany = True) 错误“[SQL 服务器的 ODBC 驱动程序 17] 转换规范的字符值无效 (0) (SQLExecute)')” - (fast_executemany = True) Error "[ODBC Driver 17 for SQL Server]Invalid character value for cast specification (0) (SQLExecute)')" (42000,[42000] [Microsoft][ODBC Driver 17 for SQL 服务器][SQL Server] s. (102) (SQLExecDirectW) 附近的语法不正确) - (42000, [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near s. (102) (SQLExecDirectW)) 无法在 python 中打开 lib 'ODBC Driver 17 for SQL Server' - Can't open lib 'ODBC Driver 17 for SQL Server' in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM