-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_db.py
More file actions
executable file
·37 lines (26 loc) · 1.08 KB
/
dump_db.py
File metadata and controls
executable file
·37 lines (26 loc) · 1.08 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
#!/usr/bin/python3
import sqlite3
import csv
import argparse
def dump_table_to_csv(db_file, table_name, csv_file):
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
# Get column names from the specified table
cursor.execute(f"PRAGMA table_info({table_name})")
column_names = [column[1] for column in cursor.fetchall()]
# Execute a query to select data from the specified table
cursor.execute(f"SELECT * FROM {table_name}")
rows = cursor.fetchall()
# Write the selected data to the CSV file
with open(csv_file, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(column_names)
writer.writerows(rows)
conn.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Dump SQLite table to a CSV file.")
parser.add_argument("db_file", help="Path to the SQLite database file")
parser.add_argument("csv_file", help="Path to the CSV file to create")
args = parser.parse_args()
table_name = 'riders'
dump_table_to_csv(args.db_file, table_name, args.csv_file)