PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 27, 2022

[FIXED] How to upload data from csv to database(PostgreSQL) on python

 August 27, 2022     csv, database, postgresql, python, sqlalchemy     No comments   

Issue

I have a few parsers that collect data and make csv file, after collecting data I need to upload data from csv to my database(PostgreSQL)

p.s.table in database is already exist and just need to append data

How can I do this?

I have try to use sqlalchemy, but after connection don't know what to do

engine = create_engine('postgresql://postgres:username@localhost:5432/DB_name')

Didn't find information that could help me


Solution

Probably you are learning because you didn't check the google before. I recommand to study the following:

https://docs.python.org/3/library/csv.html#csv.DictReader https://docs.sqlalchemy.org/en/14/tutorial/data_insert.html#tutorial-core-insert

So basically you have to do something like this:

from sqlalchemy import insert
import csv

with open('names.csv', newline='') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        insert('user_table').values(name=row['name'], fullname=row['fullname'])

Then commit!

Good luck!



Answered By - Claudiu
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing