Berkay Dedeoğlu
Berkay Dedeoğlu

Reputation: 67

SQLite3 Unknown Error

I am Using python3.

self.cur.execute("""INSERT or IGNORE INTO {0}(Konu, KonuAnlatımı, SoruSayısı, ToplamDogru, ToplamYanlıs) VALUES
                            ('{1}', '{2}', '{3}','{4}', '{5}') UPDATE {0} SET (KonuAnlatımı = '{2}'),
                            SoruSayısı = '{6}',
                            TaplamDogru = '{7}',
                            ToplamYanlıs = '{8}'
                            WHERE Konu = {1}""".format(ders, konu, Çalışıldı, soru, dogru, yanlis,
                                                       str(int(soru) + int(self.cur.execute(
                                                           "Select SoruSayısı From "+ders+" WHERE Konu = '"+konu+"'").fetchone()[0])),
                                                       str(int(dogru) + int(self.cur.execute(
                                                           "Select ToplamDogru From "+ders+" WHERE Konu = '"+konu+"'").fetchone()[0])),
                                                       str(int(dogru) + int(self.cur.execute(
                                                           "Select ToplamYanlıs From "+ders+" WHERE Konu = '"+konu+"'").fetchone()[0]))))

I get an errow which is :

"Select ToplamYanlıs From "+ders+" WHERE Konu = '"+konu+"'").fetchone()[0]))))

sqlite3.Warning: You can only execute one statement at a time.

if I delete ';' I get this:

"Select ToplamYanlıs From "+ders+" WHERE Konu = '"+konu+"'").fetchone()[0]))))

sqlite3.OperationalError: near "UPDATE": syntax error

So what is happening? And I haven't find the title name. sorry...

Upvotes: 0

Views: 971

Answers (2)

totoro
totoro

Reputation: 2456

I would suggest breaking it down to smaller parts first, for debugging, and build it up again when each part is working as expected.

I have taken the liberty to replace non-ASCII characters with visually similar ASCII versions for this example.

SoruSayisi = self.cur.execute(
    "SELECT SoruSayisi From {} WHERE Konu = ?".format(ders), konu
).fetchone()[0]

TaplamDogru = self.cur.execute(
    "SELECT ToplamDogru From {} WHERE Konu = ?".format(konu), konu
).fetchone()[0]

ToplamYanlis = self.cur.execute(
    "SELECT ToplamYanlis FROM {} WHERE Konu = ?".format(konu), konu
) .fetchone()[0]

self.cur.execute("""\
INSERT or IGNORE INTO {0}
(Konu, KonuAnlatımı, SoruSayisi, ToplamDogru, ToplamYanlis)
VALUES (?, ?, ?, ?, ?)""".format(ders),
                 konu,
                 Calisildi,
                 soru,
                 dogru,
                 yanlis)

self.cur.execute("""
UPDATE {0}
SET KonuAnlatımı = ?,
    SoruSayisi = ?,
    TaplamDogru = ?,
    ToplamYanlis = ?
WHERE Konu = {1}""".format(ders),
                 Calisildi,
                 str(int(soru) + int(SoruSayisi)),
                 str(int(dogru) + int(TaplamDogru)),
                 str(int(dogru) + int(ToplamYanlis)))

Since I cannot test this, I may have made mistakes. It's just an approach to finding out what is going on.

Upvotes: 1

johntellsall
johntellsall

Reputation: 15170

  1. Don't use Python {num} formatting, it won't quote correctly for SQL. Use the ? placeholder instead:

    c.executemany('INSERT INTO stocks VALUES (?,?)', (12, 'whiskey'))

  2. Do the sub selects separately, verify the values, then use them in the larger SQL statement -- it's clearer and simpler.

https://docs.python.org/3/library/sqlite3.html

Upvotes: 0

Related Questions