Python 在PostgreSQL表中插入多个项

Python 在PostgreSQL表中插入多个项,python,postgresql,Python,Postgresql,我想在PostgreSQL表中插入多行。我附加了这个程序。我使用了一个项目列表:new_record=['one','two','three']。 事实上,这个列表将包含来自亚马逊产品的100000个项目,但现在我填充了3个项目。我创建了一个for循环进行迭代,但它只填充了一个元素。 我想打印所有项目。例如: "INSERT INTO products(products_amazon) VALUES ('one')" "INSERT INTO products(products_amazon)

我想在PostgreSQL表中插入多行。我附加了这个程序。我使用了一个项目列表:new_record=['one','two','three']。 事实上,这个列表将包含来自亚马逊产品的100000个项目,但现在我填充了3个项目。我创建了一个for循环进行迭代,但它只填充了一个元素。 我想打印所有项目。例如:

"INSERT INTO products(products_amazon) VALUES ('one')"
"INSERT INTO products(products_amazon) VALUES ('two')"
"INSERT INTO products(products_amazon) VALUES ('three')"
我将感谢任何帮助

import psycopg2
from pprint import pprint

database = "amazon_products"
host = "localhost"
user = "postgres"
password = "123QQsuccess"


class DatabaseConnection:
    def __init__(self):
        try:
            self.connection = psycopg2.connect(database=database, host=host, user=user, password=password)
            self.connection.autocommit = True
            self.cursor = self.connection.cursor()
        except:
            pprint('Cannot connect to database')

    def insert_new_record(self):
        new_record = ['one','two','three']
        for item in new_record:
            insert_command = "INSERT INTO products(products_amazon) VALUES ('" + item + "')"
        pprint(insert_command)
        self.cursor.execute(insert_command)

if __name__ == '__main__':
    database_connection = DatabaseConnection()
    database_connection.insert_new_record()

你这样试过吗。?我希望这对你有用

INSERT INTO products(products_amazon) VALUES ('one'), ('two'), ('three')....

你这样试过吗。?我希望这对你有用

INSERT INTO products(products_amazon) VALUES ('one'), ('two'), ('three')....

如果出于任何原因,您确实希望一次插入所有语句,只需使用连接所有insert语句即可并将其作为单个字符串发送到数据库

像这样:

"INSERT INTO products(products_amazon) VALUES ('one');
INSERT INTO products(products_amazon) VALUES ('two');
INSERT INTO products(products_amazon) VALUES ('three');"

如果出于任何原因,您确实希望一次插入所有语句,只需使用连接所有insert语句即可并将其作为单个字符串发送到数据库

像这样:

"INSERT INTO products(products_amazon) VALUES ('one');
INSERT INTO products(products_amazon) VALUES ('two');
INSERT INTO products(products_amazon) VALUES ('three');"

如何使用100000个项目?您可以运行一个循环并将其附加到准备好的查询中。因为插入很简单,所以速度非常快。但为了安全起见,将项目分块到10000并进行多个查询。实际上,正如您在代码中看到的,我使用for循环,但它只添加了最后一个元素。如何使用for循环插入所有项目?您还可以尝试从postgress的
复制
将myTable从“/path/to/file/on/server”(格式为CSV,分隔符(“|”)您正在为每个项目运行查询,您应该准备一个批量查询并最终运行查询我如何使用100000个项目?您可以运行一个循环并将其附加到准备好的查询中。因为插入很简单,所以速度非常快。但为了安全起见,将项目分块到10000并进行多个查询。实际上,正如您在代码中看到的,我使用for循环,但它只添加了最后一个元素。如何使用for循环插入所有项目?您还可以尝试从postgress的
复制
将myTable从“/path/to/file/on/server”(格式为CSV,分隔符(“|”)您正在为每个项目运行查询,而应该准备一个批量查询并最终运行查询。我想从Amazon向postgresql表中插入大约100000个项目,我必须从products linksWell返回的列表中插入项目。我不熟悉Amazon的products links,但是关于向表中插入100k项,您没有很多选择:1)从文本文件复制2)Jestin建议的紧凑INSERT语句,以及3)如上所述由分号连接的INSERT语句链。您所要做的就是将您的列表(使用循环)转换为带有INSERT语句的巨大字符串,当离开循环时,您在数据库中执行该字符串。很抱歉,我无法帮助您使用python。我想在postgresql表中插入来自Amazon的大约100000个项目,并且我必须插入产品链接返回的列表中的项目。我不熟悉Amazon的产品链接,但是关于向表中插入100k项,您没有很多选择:1)从文本文件复制2)Jestin建议的紧凑INSERT语句,以及3)如上所述由分号连接的INSERT语句链。您所要做的就是将您的列表(使用循环)转换为带有INSERT语句的巨大字符串,当离开循环时,您在数据库中执行该字符串。很抱歉,无法帮助您处理python问题。。