Insert python list into PostgreSQL database

10,671

You tried to insert a list of authors into a table of posts ? That doesn't quite make sense.

Surely you're looking for something like this

data = [(1, 'post', 'author'), (2, 'post', 'author')]
for d in data:
    cur.execute("INSERT into posts(id, post, author) VALUES (%s, %s, %s)", d)

Also id values are typically integers, but you seem to be formatting as a string

If you want to keep individual lists, check out the result of zip(posts, authors)

Share:
10,671
zubmic
Author by

zubmic

Updated on July 18, 2022

Comments

  • zubmic
    zubmic almost 2 years

    I'm working on a scraping project and I'm struggling with inserting data into database.

    My database has three columns: id, post and author Scraped data is stored in two lists. First one is list posts and second one is authors. Both are the same length always because there are no articles without content or articles without author.

    I've tried solution provided in this question but I got this error:

    Traceback (most recent call last):
    File "scrape.py", line 69, in <module>
    cur.execute("INSERT into posts(id, post, author) VALUES (%s, %s, %s)", authors)
    TypeError: not all arguments converted during string formatting
    

    My lists look like this:

    author = [n1, n2, n3 ... n45]
    posts = [n1, n2, n3 ... n45]
    

    And I would like to insert them into database like this:

    ID | Post | Author
    ---|------|--------
    1  | Text | Author
    2  | Text | Author
    

    Later on I would need to extract stats from values in post column. To be exact word and number of how many times they appear but I guess it qualifies for another question.