I have a simple function that inserts a Python dictionary into DuckDB. How can I insert it into my table without creating a temporary file?
def save_to_duckdb(data):
# Connect to the Duckdb database
conn = duckdb.connect('nodes_log_duck.db')
# Get the table name from the "name" field in the dictionary
table_name = data.get('name')
# Create a temp file
file_name = table_name + str(int(time.time()))
with open( file_name,"w") as file:
json.dump(data,file)
# Create the table if it doesn't exist
conn.execute(f" CREATE TABLE IF NOT EXISTS {table_name} as SELECT * FROM read_json_auto({file_name});")
# Insert the dictionary data into the table
conn.execute(f"INSERT INTO {table_name} FROM (SELECT * FROM read_json_auto({file_name}))")
# Commit the changes to the database and close the connection
conn.commit()
conn.close()