-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path45db_delete.py
More file actions
40 lines (35 loc) · 1.3 KB
/
45db_delete.py
File metadata and controls
40 lines (35 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
import sqlite3
dbPath = "DBtesty/db02.sqlite"
try:
con = sqlite3.connect(dbPath)
print("OK: connected to:", dbPath)
except sqlite3.Error as error:
print("Problem to connect to:", dbPath)
# delete last record
try:
with con:
cur = con.cursor()
tableNameStr = "tabulka01"
query = "SELECT * FROM {} ORDER BY id DESC LIMIT 1".format(
tableNameStr)
lastRecord = cur.execute(query).fetchone()
if (lastRecord):
lastRecordNum = lastRecord[0]
print(lastRecord)
query = "SELECT seq FROM sqlite_sequence WHERE name = ?"
tableName = (tableNameStr,)
maxRowId = cur.execute(query, tableName).fetchone()
maxRowIdNum = maxRowId[0]
print(maxRowId)
query = "DELETE FROM {} WHERE id = ?".format(tableNameStr)
cur.execute(query, maxRowId)
print("OK: deleted last record")
query = "UPDATE sqlite_sequence SET seq = ? WHERE name = ?"
updateSequence = (maxRowIdNum - 1, tableNameStr)
cur.execute(query, updateSequence)
print("OK: sqlite sequence updated")
else:
print("there is no more records")
except sqlite3.Error as error:
print("Problem with simple query:", error)