forked from jacklevin74/xenminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_builder.py
54 lines (45 loc) · 1.52 KB
/
index_builder.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
import sqlite3
import json
from datetime import datetime
# Connect to the SQLite database
conn = sqlite3.connect("blockchain.db")
cursor = conn.cursor()
# Create the 'blocks' table if it doesn't exist
cursor.execute("""
CREATE TABLE IF NOT EXISTS blocks (
block_id INTEGER PRIMARY KEY AUTOINCREMENT,
hash_to_verify TEXT,
key TEXT UNIQUE,
account TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
# Fetch all records from the 'blockchain' table
cursor.execute("SELECT records_json FROM blockchain")
records = cursor.fetchall()
# Placeholder for batch inserts
all_values = []
# Loop through each record and prepare data for batch insertion into the 'blocks' table
for record in records:
records_json = record[0]
records_list = json.loads(records_json) # Assuming records_json is in JSON format
for item in records_list:
hash_to_verify = item.get("hash_to_verify")
key = item.get("key")
account = item.get("account")
created_at = item.get("date")
all_values.append((hash_to_verify, key, account, created_at))
# Batch insert into 'blocks' table
try:
cursor.executemany("""
REPLACE INTO blocks (hash_to_verify, key, account, created_at)
VALUES (?, ?, ?, ?)
""", all_values)
conn.commit()
except sqlite3.IntegrityError as e:
print(f"Integrity Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
print(f"Processed {len(all_values)} records.")
# Close the database connection
conn.close()