Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
心理学:可以';t使用python从csv复制到postgresql,没有结果_Python_Postgresql_Csv_Psycopg2 - Fatal编程技术网

心理学:可以';t使用python从csv复制到postgresql,没有结果

心理学:可以';t使用python从csv复制到postgresql,没有结果,python,postgresql,csv,psycopg2,Python,Postgresql,Csv,Psycopg2,我有一个简单的带oid的postgresql数据库。列数及其名称与csv文件中的标题相似。 我正在尝试使用“复制”命令: def db_copy_images(self, file): my_file = open(file) try: self.process_file('images_original', my_file) finally: pass def process_file(self, table, csv_file):

我有一个简单的带oid的postgresql数据库。列数及其名称与csv文件中的标题相似。 我正在尝试使用“复制”命令:

def db_copy_images(self, file):

    my_file = open(file)

    try:
        self.process_file('images_original', my_file)
    finally:
        pass

def process_file(self, table, csv_file):

    dbname = 'v'
    user = 'on'
    password = 'on'
    host = 'ip'
    port = 5432

    SQL_STATEMENT = """COPY %s FROM STDIN WITH DELIMITER AS '|' CSV HEADER"""

    print ("OK")
    conn = psycopg2.connect(database=dbname,user=user,password=password,host=host,port=port)
    cursor = conn.cursor()

    for line in csv_file:
        print(line)


    #cursor.copy_expert(sql = SQL_STATEMENT % table, file = csv_file)
    cursor.copy_expert("COPY images_original FROM STDIN WITH DELIMITER AS '|' CSV HEADER", file = csv_file)
    cursor.execute("COMMIT;")
我得到一个行列表作为输出,然后我的过程就完成了。但数据库中并没有任何更改,也并没有错误。我如何调试它?这是我的跟踪:

以下是我的数据库创建:

CREATE TABLE images_original
(
  "image id" integer NOT NULL,
  "file url" character varying NOT NULL,
  "file format id" integer,
  "file format" character varying,
  "file width" integer,
  "file height" integer,
  "file quality factor" integer,
  "file bit depth" integer,
  "file margin" integer,
  "file bg color" integer,
  "file size" integer,
  "scaling factor" character varying,
  delta character varying
)
WITH (
  OIDS=TRUE
);
ALTER TABLE images_original
  OWNER TO on;
和测试文件中的第一行:

image id|file url|file format id|file format|file width|file height|file quality factor|file bit depth|file margin|file bg color|file size|scaling factor|delta
172317239|http://cps-static.r.com/2/Open/NBC/The%20Office/Steve%20Carell%204.jpg|0||2336|3504|||||949406||INS
172317239|http://cps-static.r.com/2/Open/NBC/The%20Office/_derived_jpg_q90_0x200_m0/Steve%20Carell%204.jpg|1391886|jpg|133|200|90|24|0|24|6624|5.69|INS
172317239|http://cps-static.r.com/2/Open/NBC/The%20Office/_derived_jpg_q90_155x235_m0/Steve%20Carell%204.jpg|1391887|jpg|155|232|90|24|0|24|8092|6.64|INS

谢谢你更新这个问题。在列名中使用空格有点不寻常,但这并非不可能。我尝试直接从psql命令行输入您的复制行。成功了。然后它向我扑来:

for line in csv_file:
    print(line)

csv_file.seek(0)
打印文件中的数据时,移动文件指针。seek(0)将其重置为开始。如果添加seek,或者简单地消除打印文件内容的循环,则该命令应该有效


-来自STDOUT的g

很奇怪。通常,它是从STDIN
到STDOUT
。请参见“您能否提供原始文件中的前十几行图像并编辑到您的问题中?”?此外,创建要复制到的表的sql可以很方便地找出“复制到”命令不起作用的原因。@Greg我在帖子中添加了新数据。@DanielVérité事实上我尝试了这两种方法,但忘记了编辑-修复。我尝试在没有此循环的情况下执行此复制命令,我添加它只是为了检查我的STDIN是否为空。我将脚本更改为cursor.execute(“COPY..”)
,它最初需要超级用户权限。它是有效的,但有一些限制,这对我来说并不必要。谢谢你的回答。很有趣。我运行了上面的脚本,只是删除了自引用。它填充了表格,就像你拥有它一样。
for line in csv_file:
    print(line)

csv_file.seek(0)