現在我正在處理一個帶有數據庫的項目。當我刪除一些行時,我想讓它重置,因此它們之間沒有間隔。 這是我想要的一個例子。python sqlite3 - 如何刪除一些行後重置INTEGER PRIMARY KEY ID的值
當前數據庫。
ID: 1 Date: Date Exercise: Squat Sets: 3 Reps: 3 Weight: 3.0
ID: 2 Date: Date Exercise: Squat Sets: 3 Reps: 3 Weight: 3.0
ID: 3 Date: Date Exercise: Squat Sets: 1 Reps: 2 Weight: 3.0
ID: 4 Date: Date Exercise: Squat Sets: 1 Reps: 2 Weight: 3.0
ID: 5 Date: Date Exercise: Squat Sets: 1 Reps: 2 Weight: 3.0
ID: 6 Date: Date Exercise: Squat Sets: 1 Reps: 2 Weight: 3.0
ID: 7 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
ID: 8 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
ID: 9 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
ID: 10 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
刪除行3到7,將產生......
ID: 1 Date: Date Exercise: Squat Sets: 3 Reps: 3 Weight: 3.0
ID: 2 Date: Date Exercise: Squat Sets: 3 Reps: 3 Weight: 3.0
ID: 8 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
ID: 9 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
ID: 10 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
這就是我希望它是.....
ID: 1 Date: Date Exercise: Squat Sets: 3 Reps: 3 Weight: 3.0
ID: 2 Date: Date Exercise: Squat Sets: 3 Reps: 3 Weight: 3.0
ID: 3 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
ID: 4 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
ID: 5 Date: Date Exercise: Squat Sets: 1 Reps: 1 Weight: 4.0
這裏是功能我目前有...
def delete_range(self):
"""
Deletes a range of rows in the SQL events table. The range is determined by entries in the starting and ending
DeleteRowEntry entries.
"""
starting_index = int(self.startingRowEntry.get())
ending_index = int(self.endingRowEntry.get())
conn = lite.connect('fit.db')
cursor = conn.cursor()
cursor.execute("""DELETE FROM events WHERE(id >= (?))
AND (id <= (?))""",(starting_index,ending_index))
cursor.execute("""DELETE FROM SQLITE_SEQUENCE WHERE NAME = 'events'""")
conn.commit()
conn.close
這裏的設置爲我正在進行操作。
cursor.execute('''CREATE TABLE IF NOT EXISTS events(id INTEGER PRIMARY KEY , date text, exercise_id int, sets int, reps int, weight real)''')
爲什麼用MySQL標記它,當它是一個關於SQLite的問題? – DopeGhoti
我以爲他們是一樣的東西哈哈(:(),編輯它 – user2980081
你爲什麼要這樣做?如果在其他表或數據庫之外的任何引用這些ID,你真的**希望他們保持穩定。 –