Как я могу использовать Duckdb.read_json_auto в Python без создания временного файла?Python

Программы на Python
Anonymous
Как я могу использовать Duckdb.read_json_auto в Python без создания временного файла?

Сообщение Anonymous »

У меня есть простая функция, которая вставляет словарь Python в DuckDB. Как я могу вставить его в свою таблицу, не создавая временный файл?
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()


Подробнее здесь: https://stackoverflow.com/questions/764 ... orary-file

Вернуться в «Python»