0
1

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
.prompt '⚫◗ '
.open --readonly file.duckdb
.read file.sql

.import import_example.csv test_table --skip 1 --csv
-- .mo ma
.mode markdown
.output series.md

SELECT *
FROM generate_series(5);

| generate_series |
|----------------:|
| 0               |
| 1               |
| 2               |
| 3               |
| 4               |
| 5               |

highlight

.highlight_colors layout red
.highlight_colors column_type yellow
.highlight_colors column_name yellow bold_underline
.highlight_colors numeric_value cyan underline
.highlight_colors temporal_value red bold
.highlight_colors string_value green bold
.highlight_colors footer gray
duckdb -init prompt.sql

duckdb :memory: "SELECT 42 AS the_answer"

sql

SELECT getenv('HOME') AS home;
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');

python

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
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?