0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DuckDB

Last updated at Posted at 2025-01-07
SELECT * FROM 'flights.csv';
SELECT * FROM 'todos.json';
COPY (SELECT *
	    FROM read_csv('./output.csv',AUTO_DETECT=TRUE))
  TO './output.parquet' (FORMAT 'PARQUET', CODEC 'ZSTD');
import duckdb
import mysql.connector

# Step 1: Connect to DuckDB
duckdb_con = duckdb.connect(database=':memory:')  # Use an in-memory database or specify a file

# Step 2: Connect to MySQL
mysql_con = mysql.connector.connect(
    host='',
    user='',
    password='',
    database=''
)
mysql_cursor = mysql_con.cursor()

# Step 3: Read CSV and insert data into MySQL
csv_file_path = './output.csv'

# Using DuckDB to read and insert directly
insert_query = "INSERT INTO _account (address) VALUES (%s)"  # Use %s for MySQL parameterization

# Execute the DuckDB query and fetch all results
rows = duckdb_con.execute(f"SELECT addr FROM read_csv_auto('{csv_file_path}');").fetchall()

# Insert each row into MySQL
for row in rows:
    print(row[0])
    mysql_cursor.execute(insert_query, (row[0],))  # row is a tuple, so we need to specify the first element

# Commit changes and close connections
mysql_con.commit()
mysql_cursor.close()
mysql_con.close()
duckdb_con.close()

Ref:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?