2011-06-08 105 views
4

這是我的代碼在下面,我嘗試從一個數據庫加載數據到另一個。我相信一切正常,但發生錯誤,我不知道這意味着什麼。如何解決Python 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... 
+0

感謝您的幫助。問題是問號。我必須用%s來替換它們。 – 2011-06-09 06:42:03

回答

8

the documentationexecutemany()有兩個參數。您只提供了一個(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) 
+0

我在你的回答中如此喜歡。 但現在我得到這個錯誤: 文件 「C:\ Documents和Settings \ FS107319 \我的文檔\ Python的\ mssql_abfrage.py」 39行,在 a.abfrage() 文件「C:\ Documents和設置\ FS107319 \ My Documents \ Python \ mssql_abfrage.py「,第28行,在abfrage query(」INSERT INTO'20091229global'(proddate,site,plant,unit,line,pid,mods)VALUES(?,?,? ,?,?,?,?)「) TypeError:'tuple'對象不可調用 – 2011-06-08 12:52:45

+0

@The_Fox:您可以將此作爲編輯附加到您的問題嗎?您可以更容易地在那裏格式化,而且更容易讓我們閱讀! – Johnsyweb 2011-06-08 13:01:52

+0

和@The_fox:我認爲你需要刪除'query =(「INSERT INTO ...」)前後的括號# – 2011-06-08 13:06:58

1

試試這個:

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

self.cur2.executemany(query, self.rows) 
+0

如果您希望我們忽略某些內容,只需將其刪除並避免混淆。 – 2011-06-08 13:01:56

+0

@Bryan:是的,你是對的。 – 2011-06-08 13:04:53

相關問題