一尘不染

INSERT 和 UPDATE 语句无效,即使使用 autocommit=True

sql

当我运行它时,它们不会抛出任何错误。UPDATE并且INSERT INTO查询cikis不起作用,SQL Server 上没有发生插入或更新。

当我从 SSMS 表中检查时,我正在尝试更新并插入显示为正在执行。

giris功能“更新”正常工作。

代码中显示的值SlotAd = A1和其余值。

输出:

cikti
52 AT 533
52 AT 533
1 Arsive Eklendi.
1 slotu bosaldi.

gecmis, musaitlik bool

ucret int 和 rest varchar

除主键外,一切都允许空值。

sqltrace:

exec [sys].sp_datatype_info_100 12,@ODBCVer=4
go
exec [sys].sp_datatype_info_100 -9,@ODBCVer=4
go
exec [sys].sp_datatype_info_100 -3,@ODBCVer=4
go
exec [sys].sp_datatype_info_100 93,@ODBCVer=4
go
declare @p1 int
set @p1=1
exec sp_prepexec @p1 output,N'@P1 nvarchar(18)',N'SELECT slotAd,plaka,girisTarih,girisSaat,musaitlik from slot where plaka=@P1',N'52 AT 533'
select @p1
go
declare @p1 int
set @p1=2
exec sp_prepexec @p1 output,N'@P1 nvarchar(18),@P2 nvarchar(2),@P3 nvarchar(20),@P4 nvarchar(10),@P5 nvarchar(20),@P6 nvarchar(10),@P7 int,@P8 int',N'INSERT INTO arsiv (plaka,aracTip,girisTarih,girisSaat,cikisTarih,cikisSaat,ucret,gecmis) VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8)',N'52 AT 533',N'a',N'29.06.2022',N'16:10',N'30.06.2022',N'20:13',0,0
select @p1
go
declare @p1 int
set @p1=3
exec sp_prepexec @p1 output,N'@P1 int,@P2 nvarchar(4)',N'UPDATE slot SET musaitlik=@P1 WHERE slotAd=@P2',1,N'A1'
select @p1
go
exec sp_unprepare 3
go

我没有任何进一步的信息来确定问题。我需要指导来确定问题。我检查了相关问题,没有一个解决了我的问题。

conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
                      'Server=DESKTOP-L68QR06\SQLEXPRESS;'
                      'Database=Otopark;'
                      '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.fetchval()
        mycursor.execute("UPDATE slot SET plaka=?,girisTarih=?,girisSaat=?,musaitlik=? WHERE slotAd=?",plaka1,girisTarih,girisSaat,musaitlik,slotAd)
        conn.commit()
        print(mycursor.rowcount, "Giriş Yapildi.")
        plaka="" 

def cikis(plaka1):
    global plaka
    print("cikti")
    mycursor = conn.cursor()
    mycursor.execute("SELECT slotAd,plaka,girisTarih,girisSaat,musaitlik from slot where plaka=?",(plaka1,))
    slotAd=""
    girisSaat=""
    girisTarih=""
    plaka2=""
    musaitlik= 1
    gecmis= 0
    aracTip="a"
    ucret= 0
    for x in mycursor:
        plaka2=x[1]
        girisTarih=x[2]
        girisSaat=x[3]
        slotAd=x[0]

    if (plaka1==plaka2):
        print(plaka1)
        print(plaka2,girisTarih,girisSaat,slotAd,)
        cikisTarih = datetime.datetime.now().strftime("%d.%m.%Y")
        cikisSaat = datetime.datetime.now().strftime("%H:%M")
        sqlinsert = "INSERT INTO arsiv (plaka,aracTip,girisTarih,girisSaat,cikisTarih,cikisSaat,ucret,gecmis) VALUES (?,?,?,?,?,?,?,?)"
        mycursor.execute(sqlinsert,(plaka1,aracTip,girisTarih,girisSaat,cikisTarih,cikisSaat,ucret,gecmis))
        mycursor.commit
        print(mycursor.rowcount, "Arsive Eklendi.")
        mycursor.execute("UPDATE slot SET musaitlik=? WHERE slotAd=?",musaitlik,slotAd)
        conn.commit
        print(mycursor.rowcount, "slotu bosaldi.")
        plaka=""

    else:
        print("plaka bulunamadi.")

tantuni = "52 AT 533"
cikis(tantuni)

阅读 103

收藏
2022-07-22

共1个答案

一尘不染

autocommit=True不是连接字符串的一部分,它是传递给.connect()方法的附加关键字参数:

conn = pyodbc.connect(
    connection_string,
    autocommit=True
)

更新回复:问题编辑

不,这仍然不正确。你目前有

conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
                      'Server=DESKTOP-L68QR06\SQLEXPRESS;'
                      'Database=Otopark;'
                      'Trusted_Connection=yes;'
                      'autocommit=True'
)

但你需要的是

conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
                      'Server=DESKTOP-L68QR06\SQLEXPRESS;'
                      'Database=Otopark;'
                      'Trusted_Connection=yes;',
                      autocommit=True
)
2022-07-22