Here's a way to import csv data to database using Flask, Postgres and Sqlalchemy.
@manager.command
def import_data():
file = open("filename.txt")
process_file('table_name', file)
file.closed
def process_file(table_name, file_object):
sql_statement = """
¦ COPY %s FROM STDIN WITH
¦ CSV
¦ HEADER
¦ DELIMITER AS ','
¦ """
engine = db.engine
conn = engine.raw_connection()
cursor = conn.cursor()
cursor.copy_expert(sql=sql_statement % table_name, file=file_object)
conn.commit()
No comments:
Post a Comment