2

I have a python tool that uses sqlalchemy to connect to Postgres and MYSQL. I do this by modifying only the parameters to create_engine. However, it is possible that I have to work with the DB dump in the form of a csv file. Is there any way to pass the csv to create_engine.

I could simply load the contents of the csv into a local database and pass that to sqlalchemy, but it will be great if I can completely avoid the db while dealing with csv files.

Pradeep Vairamani
  • 3,299
  • 2
  • 30
  • 52
  • Perhaps your question is answered here: http://stackoverflow.com/questions/17662631/how-to-copy-from-csv-file-to-postgresql-table-with-headers-in-csv-file – nbryans Jun 14 '16 at 21:13
  • 2
    I would rather not do that. I don't want to load the csv into a db and then pass that db to sqlalchemy. I want a way to directly pass the csv to sqlalchemy. – Pradeep Vairamani Jun 14 '16 at 21:15
  • What does it mean to "pass the csv to sqlalchemy"? – univerio Jun 14 '16 at 21:37
  • I mean that sqlalchemy should work with csv instead of a database. Ideal, csv is passed as a parameter to create_engine method of sqlalchemy. – Pradeep Vairamani Jun 14 '16 at 22:00
  • It's not possible. SQLAlchemy only emits SQL. How are you going to execute SQL against a CSV file, except by loading it into a SQL database? – univerio Jun 14 '16 at 22:08
  • I thought SQLAlchemy is converting the contents of the DB into corresponding classes and objects. If that is the case, it should be able to parse the csv (or other flatfiles) and create the corresponding objects, right? – Pradeep Vairamani Jun 14 '16 at 22:11
  • 2
    Not quite. SQLAlchemy allows you load and save rows into a DB, as though they were objects, by emitting SQL. It has no support for CSV as a backend. – univerio Jun 14 '16 at 22:14
  • oh thank you. In that case I might have to do something like this: http://stackoverflow.com/questions/2580497/database-on-the-fly-with-scripting-languages – Pradeep Vairamani Jun 14 '16 at 22:20

0 Answers0