我開始在python中使用SQL數(shù)據(jù)庫,并且我希望有多個(gè)表,我可以在其中按ID引用彼此表中的行。因此,我使用的是“testID 整數(shù)主鍵”列。列的值會(huì)根據(jù)需要增加,但是如果我刪除具有最大 ID 的行,然后添加另一個(gè)條目,它將接收之前已設(shè)置的 ID。這很有用,因?yàn)閯h除最新的行會(huì)導(dǎo)致列中的最大ID降低,這對我來說很有意義。我現(xiàn)在想知道,有沒有辦法讓數(shù)據(jù)庫記住之前設(shè)置的每個(gè)ID,而不是設(shè)置相同的ID兩次,即使從數(shù)據(jù)庫中刪除最大ID?MWE也許可以更清楚地說明這一點(diǎn):conn = sqlite3.connect("db_problem.db")c = conn.cursor()with conn: c.execute("CREATE TABLE test (" "testID integer PRIMARY KEY, " "col1 integer)") c.execute("INSERT INTO test (col1) VALUES (1)") c.execute("INSERT INTO test (col1) VALUES (2)") c.execute("DELETE FROM test WHERE col1 LIKE 1") c.execute("SELECT testID FROM test WHERE col1 LIKE 2") print(c.fetchall()) # this stays 2, even tho there is only one row left, which works fine c.execute("INSERT INTO test (col1) VALUES (3)") c.execute("DELETE FROM test WHERE col1 LIKE 3") c.execute("INSERT INTO test (col1) VALUES (4)") c.execute("SELECT testID FROM test WHERE col1 LIKE 4") print(c.fetchall()) # Here the autoincrement was set to 3 although it is the fourth entry made
蟒蛇SQLite3 auto_increment適當(dāng)增加,即使刪除最大ID
慕田峪4524236
2022-09-27 09:28:39