1. <tfoot id='F7ltw'></tfoot>
      <legend id='F7ltw'><style id='F7ltw'><dir id='F7ltw'><q id='F7ltw'></q></dir></style></legend>
      • <bdo id='F7ltw'></bdo><ul id='F7ltw'></ul>

      <small id='F7ltw'></small><noframes id='F7ltw'>

      1. <i id='F7ltw'><tr id='F7ltw'><dt id='F7ltw'><q id='F7ltw'><span id='F7ltw'><b id='F7ltw'><form id='F7ltw'><ins id='F7ltw'></ins><ul id='F7ltw'></ul><sub id='F7ltw'></sub></form><legend id='F7ltw'></legend><bdo id='F7ltw'><pre id='F7ltw'><center id='F7ltw'></center></pre></bdo></b><th id='F7ltw'></th></span></q></dt></tr></i><div id='F7ltw'><tfoot id='F7ltw'></tfoot><dl id='F7ltw'><fieldset id='F7ltw'></fieldset></dl></div>

        使用 csv 文件中的 copy_from 到 Postgres db 时,Psycopg2 不会自动生成 id

        时间:2023-08-29

            <bdo id='ifIvh'></bdo><ul id='ifIvh'></ul>

            <tfoot id='ifIvh'></tfoot><legend id='ifIvh'><style id='ifIvh'><dir id='ifIvh'><q id='ifIvh'></q></dir></style></legend>
                <tbody id='ifIvh'></tbody>
                • <small id='ifIvh'></small><noframes id='ifIvh'>

                  <i id='ifIvh'><tr id='ifIvh'><dt id='ifIvh'><q id='ifIvh'><span id='ifIvh'><b id='ifIvh'><form id='ifIvh'><ins id='ifIvh'></ins><ul id='ifIvh'></ul><sub id='ifIvh'></sub></form><legend id='ifIvh'></legend><bdo id='ifIvh'><pre id='ifIvh'><center id='ifIvh'></center></pre></bdo></b><th id='ifIvh'></th></span></q></dt></tr></i><div id='ifIvh'><tfoot id='ifIvh'></tfoot><dl id='ifIvh'><fieldset id='ifIvh'></fieldset></dl></div>
                  本文介绍了使用 csv 文件中的 copy_from 到 Postgres db 时,Psycopg2 不会自动生成 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个包含多列的 csv 文件:

                  I have a csv file that has several columns:

                  upc 日期数量客户

                  在我的 physical 表中,每行都有一个自动生成的 id 列:

                  In my physical table, I have an auto generating id column for each row:

                  id upc 日期数量客户

                  当我运行 python 脚本复制到数据库时,数据库似乎将 upc 解释为实际 id.我收到此错误消息:

                  It seems as though the db is interpreting the upc as the actual id when I run my python script to copy into the db. I'm getting this error message:

                  Error: value "1111111" is out of range for type integer
                  CONTEXT:  COPY physical, line 1, column id: "1111111"
                  

                  我以前从未尝试过,但我相信这是正确的:

                  I've never attempted this before, but I believe this is correct:

                  def insert_csv(f, table):
                      connection = get_postgres_connection()
                      cursor = connection.cursor()
                      try:
                          cursor.copy_from(f, table, sep=',')
                          connection.commit()
                          return True
                      except (psycopg2.Error) as e:
                          print(e)
                          return False
                      finally:
                          cursor.close()
                          connection.close()
                  

                  我在这里做错了什么,还是我必须创建另一个脚本才能从表中获取最后一个 id?

                  Am I doing something wrong here, or do I have to create another script to get the last id from the table?

                  更新的工作代码:

                  def insert_csv(f, table, columns):
                      connection = get_postgres_connection()
                      cursor = connection.cursor()
                      try:
                          column_names = ','.join(columns)
                          query = f'''
                              COPY {table}({column_names})
                              FROM STDOUT (FORMAT CSV)
                          '''
                          cursor.copy_expert(query, f)
                          connection.commit()
                          return True
                      except (psycopg2.Error) as e:
                          print(e)
                          return False
                      finally:
                          cursor.close()
                          connection.close()
                  

                  columns = (
                          "upc",
                          "date_thru",
                          "transaction_type",
                          "transaction_type_subtype",
                          "country_code",
                          "customer",
                          "quantity",
                          "income_gross",
                          "fm_serial",
                          "date_usage"
                      )
                  

                  with open(dump_file, 'r', newline='', encoding="ISO-8859-1") as f:
                          inserted = insert_csv(f, 'physical', columns)
                  

                  推荐答案

                  您需要指定要导入的列.来自文档:

                  You need to specify columns to import. From the documentation:

                  columns – 可与要导入的列的名称进行迭代.长度和类型应与要读取的文件的内容相匹配.如果未指定,则假定整个表与文件结构匹配.

                  columns – iterable with name of the columns to import. The length and types should match the content of the file to read. If not specified, it is assumed that the entire table matches the file structure.

                  您的代码可能如下所示:

                  Your code may look like this:

                  def insert_csv(f, table, columns):
                      connection = connect()
                      cursor = connection.cursor()
                      try:
                          cursor.copy_from(f, table, sep=',', columns=columns)
                          connection.commit()
                          return True
                      except (psycopg2.Error) as e:
                          print(e)
                          return False
                      finally:
                          cursor.close()
                          connection.close()
                          
                  with open("path_to_my_csv") as file:
                      insert_csv(file, "my_table", ("upc", "date", "quantity", "customer"))
                  

                  如果您必须使用 copy_expert(),请按以下方式修改您的函数:

                  If you have to use copy_expert() modify your function in the way as follow:

                  def insert_csv(f, table, columns):
                      connection = connect()
                      cursor = connection.cursor()
                      try:
                          column_names = ','.join(columns)
                          copy_cmd = f"copy {table}({column_names}) from stdout (format csv)"
                          cursor.copy_expert(copy_cmd, f)
                          connection.commit()
                          return True
                      except (psycopg2.Error) as e:
                          print(e)
                          return False
                      finally:
                          cursor.close()
                          connection.close()
                  

                  这篇关于使用 csv 文件中的 copy_from 到 Postgres db 时,Psycopg2 不会自动生成 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:试图在来自 DF 的切片副本上设置值 下一篇:从 Python 调用和控制 GDB

                  相关文章

                  1. <tfoot id='ZYWg1'></tfoot>

                      <i id='ZYWg1'><tr id='ZYWg1'><dt id='ZYWg1'><q id='ZYWg1'><span id='ZYWg1'><b id='ZYWg1'><form id='ZYWg1'><ins id='ZYWg1'></ins><ul id='ZYWg1'></ul><sub id='ZYWg1'></sub></form><legend id='ZYWg1'></legend><bdo id='ZYWg1'><pre id='ZYWg1'><center id='ZYWg1'></center></pre></bdo></b><th id='ZYWg1'></th></span></q></dt></tr></i><div id='ZYWg1'><tfoot id='ZYWg1'></tfoot><dl id='ZYWg1'><fieldset id='ZYWg1'></fieldset></dl></div>
                      <legend id='ZYWg1'><style id='ZYWg1'><dir id='ZYWg1'><q id='ZYWg1'></q></dir></style></legend>
                    1. <small id='ZYWg1'></small><noframes id='ZYWg1'>

                        <bdo id='ZYWg1'></bdo><ul id='ZYWg1'></ul>