python - 如何解决 Python TypeError?

标签 python psycopg2 typeerror

下面是我的代码,我尝试将数据从一个数据库加载到另一个数据库。我相信一切正常,但发生错误,我不知道这意味着什么。

import pymssql, psycopg2

class Datenbankabfrage:

def __init__(self):
    self.conn1 = pymssql.connect(host='***', user='***', password='***', database='****')
    self.conn2 = psycopg2.connect("dbname='****' user='****' host='****' password='****'")

    self.cur1 = self.conn1.cursor()
    self.cur2 = self.conn2.cursor()

def abfrage(self):
    self.cur1.execute("SELECT tag, site, plant, unit, line, ProcessID AS pid, Count(ReadTime) AS mods \
                        FROM ( \
                        select dateadd(dd, -1, convert(varchar, getDate(),111)) \
                        as tag, ReadTime, processID, subid, PR.Site, PR.Plant, PR.Unit, PR.Line \
                        from FactBarcodeReading BCR with(nolock) \
                        inner join DimProcess PR on BCR.ProcessKey = PR.ProcessKey \
                        where PR.ProcessID IN  (802, 1190, 1800, 3090, 3590, 4390, 4590, 4800, 5000, 5400, 4190) \
                        and ReadTime between dateadd(dd, -1, convert(varchar, getDate(),111)) \
                        and dateadd(dd, -0, convert(varchar, getDate(),111)) \
                        ) a \
                        GROUP BY tag, site, plant, unit, line, ProcessID \
                        ORDER BY site, plant, unit, line, ProcessID")

    self.rows = self.cur1.fetchall()

    query = ("INSERT INTO '20091229global' (proddate, site, plant, unit, line, pid, mods) VALUES (?, ?, ?, ?, ?, ?, ?)", self.rows)

    self.cur2.executemany(query)

    self.conn2.commit()

    self.conn2.close()



a = Datenbankabfrage()
a.abfrage()

这是错误:

Traceback (most recent call last):
File "C:\Documents and Settings\FS107319\My Documents\Python\mssql_abfrage.py", line 39, in <module>
a.abfrage()
File "C:\Documents and Settings\FS107319\My Documents\Python\mssql_abfrage.py", line 30, in    abfrage
self.cur2.executemany(query)
TypeError: Required argument 'vars_list' (pos 2) not found

-------------------------------------------- --------------------------

好的,这是我的编辑:

现在,这是我的新代码

query("INSERT INTO '20091229global' (proddate, site, plant, unit, line, pid, mods) VALUES ('?', '?', '?', '?', '?', '?', '?')")

self.cur2.execute(query, self.rows)

抱歉之前发生的错误是错误的,因为我忘记了查询后面的“=” 这才是真正的错误

Traceback (most recent call last):
File "C:\Documents and Settings\FS107319\My Documents\Python\mssql_abfrage.py", line 39, in <module>
a.abfrage()
File "C:\Documents and Settings\FS107319\My Documents\Python\mssql_abfrage.py", line 30, in abfrage
self.cur2.execute(query, self.rows)
ProgrammingError: FEHLER:  Syntaxfehler bei »'20091229global'«
LINE 1: INSERT INTO '20091229global' (proddate, site, plant, unit, l...

最佳答案

根据 the documentation , executemany() 有两个参数。您只提供了一个 (query)。

executemany(operation, seq_of_parameters)

Prepare a database operation (query or command) and then execute it against all parameter tuples or mappings found in the sequence seq_of_parameters.

The function is mostly useful for commands that update the database: any result set returned by the query is discarded.

Parameters are bounded to the query using the same rules described in the execute() method.

也许你只是想要execute()

或者,更有可能:

query = "INSERT INTO '20091229global' (proddate, site, plant, unit, line, pid, mods) VALUES (?, ?, ?, ?, ?, ?, ?)"
self.cur2.executemany(query, self.rows)

关于python - 如何解决 Python TypeError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6278531/

相关文章:

python - 如何在速度方面提高我的 'for' 循环性能?

python csv 两列同名

python - 如何避免模数浮点误差?

python - 如何使用 bs4 从网站获取表数据

python - 从 executemany 语句中获取 'return' d 值

python - 使用 psycopg 时表格不会改变

javascript - 从.use(req,res)更改为使用routes.js文件时,express应用程序出现错误

python - 在 Python 上加载/使用 SQL 函数(避免往返)

python - 类型错误: 'float' 对象不可迭代,Python 列表

Python 自然语言处理 : TypeError: not all arguments converted during string formatting