0

I am trying to copy the data from one table to another table. Normally using the SELECT command, we can read the whole table, and using the INSERT command we can insert the data into another table. But I don't want to use raw SQL command, I want to use SQLAlchemy ORM to copy and insert. Is there any way to do it?

1 Answers1

0

Are you just trying to add an entry to a database, or are you trying to duplicate an entry?

Adding would be done by simply doing:

ed_user = User(name='ed', fullname='Ed Jones', nickname='edsnickname')
session.add(ed_user)
session.commit()

The example was taken from the official documentation. The commit will actually write the data added to the session, to the database.

EDIT: you'll have to write something that parses the file into objects and add those objects to the database. Depends on what kind of file, if it's a database export, then you can just import with your preferred database tool. You can have a look at this blog post as well. Bottom-line is that if you want to import from csv / excel / txt, you'll have to write something for it.

T. Kelher
  • 1,188
  • 10
  • 9