簡體   English   中英

如何在Python中通過變量分配ROWID?

[英]How can I assign the ROWID through a variable in Python?

有沒有一種方法可以通過Python中的變量分配SQLite rowid? 我正在使用Tkinters的“ get()”函數來檢索條目的內容。

這是代碼:

def insertdata():
    c.execute("INSERT INTO Students VALUES (?, ?, ?, ?, ?, ?, ?, ?);", (surnamelabelentry.get(), forenamelabelentry.get(), dateofbirthlabelentry.get(), homeaddresslabelentry.get(), homephonenumberentry.get(), genderlabelentry.get(), tutorgrouplabelentry.get(), emaillabelentry.get()))
    c.execute('INSERT INTO Students(rowid) VALUES',(studentidentry.get()))
    conn.commit()
    rootE.destroy()

這是錯誤:

在insertdata c.execute('INSERT INTO Students(rowid)VALUES',(studentidentry.get()))sqlite3.OperationalError:附近“ VALUES附近”的文件“ R:/ Documents / PYTHON / Login And Pw.py”,第124行“:語法錯誤

提前致謝。

創建表時,將一列聲明為INTEGER PRIMARY KEY

CREATE TABLE Students (
    StudentID INTEGER PRIMARY KEY,
    Surname VARCHAR NOT NULL,
    Forename VARCHAR NOT NULL,
    ...
);

這使該列成為ROWID的別名,然后您可以使用常規的INSERT語句進行設置。

如果出於某種奇怪的原因,您希望將ROWID保留為隱藏列,但仍為其設置顯式值,則可以為INSERT使用顯式列列表。

c.execute("INSERT INTO Students(ROWID, Surname, Forename) VALUES (?, ?, ?)", (5678, 'Tables', 'Robert'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM