Home > Blockchain >  Sqlite3 adding ID
Sqlite3 adding ID

Time:01-26

class Personel():
    def __init__(self, adsoyad, birim, telefon, cinsiyet):
        self.adsoyad = adsoyad
        self.birim = birim
        self.telefon = telefon
        self.cinsiyet = cinsiyet

class PersonelEnvanteri():
    def __init__(self):
        self.baglanti_olustur()
    
    def baglanti_olustur(self):
        self.baglanti = sqlite3.connect("personelenvanteri.db")
        self.cursor = self.baglanti.cursor()
        sorgu = "CREATE TABLE IF NOT EXISTS personeller (adsoyad TEXT, birim TEXT, telefon TEXT, cinsiyet TEXT)"
        self.cursor.execute(sorgu)
        self.baglanti.commit()

    def baglantiyi_kes(self):
        self.baglanti.close()

    def personelEkle(self, personel):
        sorgu = "INSERT INTO personeller VALUES (?, ?, ?, ?)"
        self.cursor.execute(sorgu,(personel.adsoyad, personel.birim, personel.telefon, personel.cinsiyet))
        self.baglanti.commit()

personelenvanteri = PersonelEnvanteri()

class App (tkinter)...
.
.
.
    def personelEkleButon(self):
        adsoyad = self.personelAdGiris.get()
        birim = self.personelBirimGiris.get()
        telefon = self.personelTelGiris.get()
        if self.radiosecim == 1:
            cinsiyet = "Erkek"
        else:
            cinsiyet = "Kadın"
        yeniPersonel = Personel(adsoyad, birim, telefon, cinsiyet)
        personelenvanteri.personelEkle(yeniPersonel)
        print("Hello")

I need to use ID INTEGER PRIMARY KEY AUTOINCREMENT above, but I'm making a mistake somewhere, I couldn't run it. What exactly do I need to do in these codes, where and what should I put?

I'm getting a value missing error

CodePudding user response:

Add the ID column when you create the table. But leave it out when you're inserting, and the ID will be assigned automatically.

    def baglanti_olustur(self):
        self.baglanti = sqlite3.connect("personelenvanteri.db")
        self.cursor = self.baglanti.cursor()
        sorgu = """CREATE TABLE IF NOT EXISTS personeller (
            ID INTEGER PRIMARY KEY AUTOINCREMENT,
            adsoyad TEXT, 
            birim TEXT, 
            telefon TEXT, 
            cinsiyet TEXT)"""
        self.cursor.execute(sorgu)
        self.baglanti.commit()

    def baglantiyi_kes(self):
        self.baglanti.close()

    def personelEkle(self, personel):
        sorgu = """INSERT INTO personeller (adsoyad, birim, telefon, cinsiyet) 
                    VALUES (?, ?, ?, ?)"""
        self.cursor.execute(sorgu,(personel.adsoyad, personel.birim, personel.telefon, personel.cinsiyet))
        self.baglanti.commit()
  • Related