psycopg2不会将数据保存到Postgres数据库中

时间:2018-10-07 09:23:34

标签: python-3.x postgresql psycopg2

因此,我正在从新闻网站上抓取某些文章。而且我正在使用psycopg2连接到postgres数据库并保存本文中的数据。

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 ('')

问题是,当执行第二个查询时,它从商品表返回数据,但是当我通过终端psql进行查询时,它显示商品表为空。

1 个答案:

答案 0 :(得分:2)

尝试一下。希望对您有所帮助。

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 ('')