-
Notifications
You must be signed in to change notification settings - Fork 0
/
tosqlite.py
87 lines (79 loc) · 2.66 KB
/
tosqlite.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import sqlite3
import csv
# Define the CSV and SQLite file paths
csv_file_path = 'cleaned_data.csv' # Replace with your actual CSV file path
sqlite_db_path = 'updated_data.db' # Replace with your SQLite database file path
try:
# Connect to the SQLite database (or create it if it doesn't exist)
conn = sqlite3.connect(sqlite_db_path)
cursor = conn.cursor()
# # Create a table with all columns set to TEXT type
create_table_query = '''
CREATE TABLE data_table (
copyright_claimant TEXT,
application_title_statement TEXT,
email TEXT,
registration_number TEXT,
registration_date TEXT,
year_of_creation INT,
record_status TEXT,
physical_description TEXT,
personal_authors TEXT,
corporate_authors TEXT,
rights_note TEXT,
author_statement TEXT,
authorship TEXT
);
'''
cursor.execute(create_table_query)
print("Table created successfully!")
# Check for existing tables
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()
print("Existing tables:", tables)
# Open and read the CSV file, then insert each row into the database
with open(csv_file_path, 'r', encoding='utf-8') as csv_file:
csv_reader = csv.DictReader(csv_file)
row_count = 0
for row in csv_reader:
cursor.execute('''
INSERT OR IGNORE INTO data_table (
copyright_claimant,
application_title_statement,
email,
registration_number,
registration_date,
year_of_creation,
record_status,
physical_description,
personal_authors,
corporate_authors,
rights_note,
author_statement,
authorship
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
row['Copyright Claimant'],
row['Application Title Statement'],
row['Email'],
row['Registration Number'],
row['Registration Date'],
row['Year of Creation'],
row['Record Status'],
row['Physical Description'],
row['Personal Authors'],
row['Corporate Authors'],
row['Rights Note'],
row['Author Statement'],
row['Authorship']
))
row_count += 1
# Commit changes
conn.commit()
print(f"{row_count} rows imported successfully!")
except sqlite3.Error as e:
print(f"An error occurred: {e}")
finally:
# Ensure the connection is closed
if conn:
conn.close()