2

So I am web scraping from a news site for certain articles. And I am using psycopg2 to connect to postgres database and save data from this article.

with conn.cursor() as cur:
    query = """INSERT INTO
    articles (title, article_body, author, author_title, source_date, "createdAt", "updatedAt")
    VALUES (%s, %s, %s, %s, %s, %s, %s);"""
    cur.execute(query, (articleTitle, parsedText, articleAuthor, articleAuthorTitle, articlePostDate, now, now))
    cur.execute('SELECT author FROM articles')
    rows = cur.fetchall()
    print ('')
    print (rows)
    print ('')

The thing is that when second query is executed, it returns the data from the articles table, but when I make a query through terminal psql it shows that articles table is empty.

3
  • 1
    You should commit after you execute the insert query. Add this line conn.commit() after the line cur.execute(query, (articleTitle,... Commented Oct 7, 2018 at 9:27
  • Thanks, that was the problem. Completely forgot about commiting Commented Oct 7, 2018 at 9:33
  • 1
    Hope you don't mind if I add it as the answer. Commented Oct 7, 2018 at 9:35

1 Answer 1

4

Try this. Hope it helps.

with conn.cursor() as cur:
    query = """INSERT INTO
    articles (title, article_body, author, author_title, source_date, "createdAt", "updatedAt")
    VALUES (%s, %s, %s, %s, %s, %s, %s);"""
    cur.execute(query, (articleTitle, parsedText, articleAuthor, articleAuthorTitle, articlePostDate, now, now))
    conn.commit()
    cur.execute('SELECT author FROM articles')
    rows = cur.fetchall()
    print ('')
    print (rows)
    print ('')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.