[英]Python SQLite3 Updating primary key when another entry is deleted
Currently I am using a database for a student music lesson planner. 目前我正在为学生音乐课程计划者使用数据库。
Whenever I delete a student from the database and then I add a new student why doesn't it add the new student with the ID of 1? 每当我从数据库中删除一个学生然后我添加一个新学生为什么不添加ID为1的新学生?
The code for adding data is as follows: 添加数据的代码如下:
def add():
add = 'y'
while add == 'y':
Name = input("Please enter the name of the student: ")
Year = int(input("Please enter the students year: "))
Student = (Name,Year)
insert_data(Student)
print("New data added")
print()
add = input("Do you wish to add another student? (y/n) ")
display = menu.Menu.DisplayMenu("Student") #Adding student name to menu
choice = GetMenuChoice() #Getting the user's menu choice
ValidateMenuChoice(choice) #Validating that user choice
main(choice) #Passing in choice into the main variable
def insert_data(values):
with sqlite3.connect("records.db")as db:
cursor = db.cursor()
sql = "Insert into Student(Name,Year)values(?,?)"
cursor.execute(sql,values)
db.commit()
and the database code is: 而数据库代码是:
def create_student_table():
sql = """create table Student
(StudentID integer,
Name string,
Year integer,
primary key(StudentID))"""
create_table(db_name,"Student",sql)
Any help would be greatly appreciated! 任何帮助将不胜感激!
That's how most databases work; 这就是大多数数据库的工作方式; sequences only ever produce new values and not reuse old values.
序列只生成新值而不重用旧值。
This is both more efficient (not having to search for free ids) and a good idea. 这更有效(不必搜索免费ID)和一个好主意。 Imagine a system that integrates multiple parts, including your database.
想象一个集成了多个部分的系统,包括您的数据库。 Then the User ID you just deleted could still be referenced in other systems as well.
然后,您刚删除的用户ID仍可在其他系统中引用。 If your database would reuse the ID when creating a new user, suddenly those external systems are referring to a new user, leading to hard-to-untangle bugs.
如果您的数据库在创建新用户时会重用该ID,那么这些外部系统突然指的是新用户,从而导致难以解决的错误。
If you must reuse numbers, then you'll have to specify an id to use explicitly instead of having the database pick one for you. 如果必须重复使用数字,那么您必须指定一个显式使用的ID,而不是让数据库为您选择一个。 You'll have to find your own 'missing' numbers in that case, which will be difficult if you are using the database from more than one thread or process.
在这种情况下,您必须找到自己的“缺失”数字,如果您使用来自多个线程或进程的数据库,这将很难 。
Also see the SQLite auto-incrementation documentation for some more detail on how you can influence how new values are picked; 另请参阅SQLite自动增量文档 ,了解有关如何影响新值的选择的更多详细信息; you either get monotonically increasing numbers (which can produce gaps) or, if you add the
AUTOINCREMENT
keyword, a value to be guaranteed to be 'highest value ever used plus 1'. 你要么得到单调增加的数字(可能产生间隙),要么加上
AUTOINCREMENT
关键字,要保证一个值是'曾经使用的最高值加1'。
Note that the default behaviour is to automatically generate a ROWID
that is 1 higher than the values currently in use: 请注意,默认行为是自动生成比当前使用的值高1的
ROWID
:
If no
ROWID
is specified on the insert, or if the specifiedROWID
has a value ofNULL
, then an appropriateROWID
is created automatically.如果没有
ROWID
在插入件被指定,或者如果指定ROWID
具有值NULL
,则适当的ROWID
被自动创建。 The usual algorithm is to give the newly created row aROWID
that is one larger than the largestROWID
in the table prior to the insert.通常的算法是为新创建的行提供一个
ROWID
,该ROWID
比插入前表中的最大ROWID
大一个 。The normal
ROWID
selection algorithm described above will generate monotonically increasing uniqueROWIDs
as long as you never use the maximum ROWID value and you never delete the entry in the table with the largestROWID
.正常的
ROWID
上述选择算法将产生单调递增独特ROWIDs
只要你不使用的最大ROWID值,你永远不删除具有最大的表中的条目ROWID
。 If you ever delete rows or if you ever create a row with the maximum possibleROWID
, thenROWIDs
from previously deleted rows might be reused when creating new rows and newly createdROWID
s might not be in strictly ascending order.如果你删除行或如果你曾经创建的最大可能的行
ROWID
,那么ROWIDs
从以前删除的行可能会创建新行时,再利用,新创建的ROWID
S可能会发生无法在严格按升序排列。
Emphasis mine. 强调我的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.