2

When I run it, they don't throw any errors. UPDATE and INSERT INTO query at cikis do not work, there is no insertion or update happening at SQL Server.

When I check from SSMS tables I am trying to update and insert showing as executing.

At giris function "UPDATE" works properly.

Value of SlotAd = A1 and rest of values shown in code.

Output:

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

gecmis, musaitlik bool

ucret int and rest varchar

Everything allow nulls except primary keys.

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

I don't have any further information to determine the problem. I need guidance to determine the problem. I checked relevant questions non of them fixed my problem.

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)
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) (and especially so when those links are to externally hosted images). – Thom A Jun 30 '22 at 16:02
  • Okay removing them – user19435919 Jun 30 '22 at 16:10
  • Don't just remove them; replace the data with consumable data. – Thom A Jun 30 '22 at 16:15
  • I removed them and added information that images give. Sorry Im new as Question Poster. Usually I find what Im looking for from other question answers. This time non of them did work. – user19435919 Jun 30 '22 at 16:16
  • Either your code is not actually executing your queries or you have something that is swallowing your errors or rolling back the transaction. Have your tried running a sql trace or an extended events session to see if your queries are actually running? – Sean Lange Jun 30 '22 at 17:03
  • edited post with trace data. – user19435919 Jun 30 '22 at 17:32

1 Answers1

2

autocommit=True is not part of the connection string, it is an additional keyword argument passed to the .connect() method:

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

Update re: question edit

No, that's still not correct. You currently have

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

but what you need is

conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
                      'Server=DESKTOP-L68QR06\SQLEXPRESS;'
                      'Database=Otopark;'
                      'Trusted_Connection=yes;',
                      autocommit=True
)
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418