2
插入id 1和2.删除id 2.插入新行,它会得到id 2而不是3.如何更改此?我不想重用主键。不要使用已删除的主键
import sqlite3, os
os.remove('mydatabase.db')
conn = sqlite3.connect("mydatabase.db") # or use :memory: to put it in RAM
cursor = conn.cursor()
# create a table
cursor.execute("CREATE TABLE if not exists albums (id INTEGER PRIMARY KEY NOT NULL, title text, artist text, release_date text, publisher text, media_type text)")
cursor.execute("CREATE TABLE if not exists anti (id INTEGER, title text, artist text, release_date text, publisher text, media_type text)")
conn.commit()
cursor.execute("INSERT INTO albums VALUES (null, 'Glow', 'Andy Hunter', '7/24/2012', 'Xplore Records', 'MP3')")
cursor.execute("INSERT INTO albums VALUES (null, 'second', 'artist', '7/24/2012', 'Xplore Records', 'MP3')")
conn.commit()
cursor.execute("SELECT * FROM albums")
print(cursor.fetchall())
cursor.execute("INSERT INTO anti SELECT * FROM albums WHERE title='second'")
cursor.execute("DELETE FROM albums WHERE title='second'")
cursor.execute("INSERT INTO albums VALUES (null, 'third', 'artist', '7/24/2012', 'Xplore Records', 'MP3')")
conn.commit()
cursor.execute("SELECT * FROM albums")
print(cursor.fetchall())
cursor.execute("SELECT * FROM anti")
print(cursor.fetchall())
这个问题值得追究吗?我没有看到任何重复的信息 – user193661
是的,我认为是。我认为你不应该觉得你需要删除一个问题,如果它是重复的。问题不断以重复方式关闭,但未被删除。如果有重复的话,这里的措词可能会对搜索引擎更友好。 –
@ user193661你很幸运; [其他问题](http://stackoverflow.com/questions/33135324/sqlite-prevent-primary-key-value-from-resetting-after-delete-all-rows?lq=1)不太一般。 –