Skip to content

Commit 957aa62

Browse files
committed
extract nodes from db
1 parent 8fbcff9 commit 957aa62

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

create_nodes_database.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import sqlite3
2+
3+
# Source and destination database paths
4+
source_db_path = "mqtt_messages.db"
5+
dest_db_path = "mqtt_messages_nodes.db"
6+
7+
# Connect to source database
8+
source_conn = sqlite3.connect(source_db_path)
9+
source_cursor = source_conn.cursor()
10+
11+
# Connect to destination database
12+
dest_conn = sqlite3.connect(dest_db_path)
13+
dest_cursor = dest_conn.cursor()
14+
15+
# Get the nodes table schema
16+
columns = source_cursor.execute("PRAGMA table_info(nodes)").fetchall()
17+
column_names = [col[1] for col in columns]
18+
19+
# Create table creation and insert statements dynamically
20+
create_table_sql = f"""CREATE TABLE IF NOT EXISTS nodes (
21+
{', '.join([f"{col[1]} {col[2]}" for col in columns])}
22+
)"""
23+
dest_cursor.execute(create_table_sql)
24+
25+
# Fetch nodes data
26+
nodes_data = source_cursor.execute("SELECT * FROM nodes").fetchall()
27+
28+
# Prepare insert statement dynamically
29+
insert_columns = ', '.join(column_names)
30+
placeholders = ', '.join(['?' for _ in column_names])
31+
insert_sql = f"INSERT INTO nodes ({insert_columns}) VALUES ({placeholders})"
32+
33+
# Insert nodes data into destination database
34+
dest_cursor.executemany(insert_sql, nodes_data)
35+
36+
# Commit changes and close connections
37+
dest_conn.commit()
38+
source_conn.close()
39+
dest_conn.close()
40+
41+
print(f"Nodes table copied to {dest_db_path}. Total nodes: {len(nodes_data)}")

0 commit comments

Comments
 (0)