使用Python将数据从一个Postgresql服务器添加到另一个Postgresql服务器

时间:2017-01-04 09:53:43

标签: python postgresql psycopg2

我对python和postgresql很新,但是我在将数据从一台服务器传输到另一台服务器时遇到了问题。目前我有代码运行我拉数据并将其设置为变量IT,当我尝试将IT插入另一个Postgresql服务器时,我遇到了错误。

首先我用过:

cur = con.cursor()
#Connect cursor to local server
IT = DataPull()
#Pulls the data from the remote Postgresql server and set it equal to IT

command2 = (
    """
    INSERT INTO gr_data.it (column_name1,column_name2,column_name3,column_name4,column_name5,column_name6,column_name7) VALUES(?,?,?,?,?,?,?)
    """)
cur.execute(command2, IT)

但我最终收到错误:

psycopg2.ProgrammingError: syntax error at or near ","
LINE 2:...column_name4,column_name5,column_name6,column_name7 VALUES<?,?,?,?,?....
                                                                      ^

所以我认为它与问号有关,我用Google搜索并发现可能它们应该更改为“%s”。然后我收到了这个错误:

TypeError: not all arguments converted during string formatting

任何帮助?

1 个答案:

答案 0 :(得分:0)

以下是如何使用变量执行插入的示例:

cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abc'def"))

取自here

的官方文档