3

I would like to load CSV file into SQLite coded using SQLAlchemy (to fit other code).

The command line load looks like this:

create table MYTABLE (...);
.separator ','
.import myfile.csv MYTABLE

(Avoiding per row INSERTs) Is there an equivalent SQLAlchemy operation?

Thanks

1

1 Answer 1

3

The .import command is part of the CLI and not part of the SQLite syntax.

If you want to import the content of the CSV file you can use the python's csv reader and using SQLAlchemy to insert every row (from the CSV) to the database:

>>> import csv
>>> with open('myfile.csv', 'rb') as csvfile:
...     tbl_reader = csv.reader(csvfile, delimiter=',')
...     for row in tbl_reader:
...         mytbl.insert().values(id=row[0], name=row[1], content=row[2])
Sign up to request clarification or add additional context in comments.

3 Comments

How about start a transaction and commit every 100 (or 500) inserts?
I will see how transaction improves it. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.