30 lines
1008 B
Python
30 lines
1008 B
Python
import sqlite3
|
||
|
||
|
||
def ClearTableRecordsWithReset(db_path, table_name):
|
||
"""
|
||
清空指定表的记录,并重置自增ID。
|
||
|
||
:param db_path: str, SQLite 数据库路径
|
||
:param table_name: str, 表名
|
||
"""
|
||
conn = sqlite3.connect(db_path)
|
||
cursor = conn.cursor()
|
||
|
||
try:
|
||
cursor.execute(f"DELETE FROM {table_name};")
|
||
cursor.execute(f"DELETE FROM sqlite_sequence WHERE name='{table_name}';")
|
||
conn.commit()
|
||
print(f"表 [{table_name}] 已清空并重置自增ID")
|
||
except sqlite3.Error as e:
|
||
print(f"❌ 操作失败: {e}")
|
||
finally:
|
||
conn.close()
|
||
|
||
|
||
if __name__ == '__main__':
|
||
# ClearTableRecordsWithReset(db_path='../src/db_ntfs_info.db', table_name='db_path')
|
||
# ClearTableRecordsWithReset(db_path='../src/db_ntfs_info.db', table_name='db_device')
|
||
# ClearTableRecordsWithReset(db_path='../src/db_ntfs_info.db', table_name='db_config')
|
||
ClearTableRecordsWithReset(db_path='../src/db_ntfs_info.db', table_name='db_node')
|