-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
47 lines (37 loc) · 1.59 KB
/
main.py
File metadata and controls
47 lines (37 loc) · 1.59 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
41
42
43
44
45
46
47
import os
import sqlite3
def clear_db_data(folder_path):
"""
Clears data from all SQLite databases in the specified folder
while preserving the schema/structure.
Parameters:
folder_path (str): Path to the folder containing SQLite database files.
"""
if not os.path.isdir(folder_path):
print(f"The specified path '{folder_path}' is not a valid directory.")
return
# Iterate through all files in the folder
for file_name in os.listdir(folder_path):
if file_name.endswith('.db'): # Check for .db files
db_path = os.path.join(folder_path, file_name)
try:
# Connect to the SQLite database
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Get a list of all tables in the database
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
# Delete data from each table
for table_name, in tables:
cursor.execute(f"DELETE FROM {table_name};")
print(f"Cleared data from table '{table_name}' in database '{file_name}'.")
# Commit changes and close the connection
conn.commit()
conn.close()
except sqlite3.Error as e:
print(f"An error occurred while processing '{file_name}': {e}")
print("Data clearing complete.")
# Specify the folder containing your .db files
folder_with_dbs = r"C:\Path\to\db\folder"
# Call the function
clear_db_data(folder_with_dbs)