Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DuckDB - efficiently insert pandas dataframe to table with sequence

CREATE TABLE temp (
    id UINTEGER,
    name VARCHAR,
    age UINTEGER
);
CREATE SEQUENCE serial START 1;

Insertion with series works just fine:

INSERT INTO temp VALUES(nextval('serial'), 'John', 13)

How I can use the sequence with pandas dataframe?

data = [['Alex',10],['Bob',12],['Clarke',13]]
df = pd.DataFrame(data,columns=['Name','Age'])
print(df)
     Name  Age
0    Alex   10
1     Bob   12
2  Clarke   13

con.execute("INSERT INTO temp SELECT * FROM df")
RuntimeError: Binder Error: table temp has 3 columns but 2 values were supplied

I don't want to iterate item by item. The goal is to efficiently insert 1000s of items from python to DB. I'm ok to change pandas to something else.

like image 550
Dmitry Petrov Avatar asked Mar 06 '26 04:03

Dmitry Petrov


1 Answers

Can't you have nextval('serial') as part of your select query when reading the df?

e.g.,

con.execute("INSERT INTO temp SELECT nextval('serial'), Name, Age FROM df")
like image 185
Pedro Holanda Avatar answered Mar 08 '26 17:03

Pedro Holanda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!