2014-09-02 71 views
0

我需要你的幫助,通過python更新我的mysqldb中的數據。一切正常,包括閱讀,插入等 下面的查詢不工作...mysqldb更新函數Python錯誤str

cursor.execute( 「UPDATE einzelcheck SET Kursbuch =%s的WHERE analysenNummer =%S」(十進制(航向對接)中,i [8]))

我試圖與幾個optiosn爲航向對接和我[8],總是我得到以下信息:

cursor.execute(「UPDATE einzelanalyseanalysen SET Kurs bei empf =%S其中。 analysenNummer =%s「(十進制(kurs),i [12])) TypeError:'str'對象不可調用

str(i [8])也不起作用。

「kurs」是小數。在程序中,我使用了十進制(kurs),我可以計算沒有問題。要寫入數據庫「Kurs bei empf」的值具有十進制格式(10,2)

i [12]是我之前提取的數據庫條目的一部分。在數據庫中的格式是int(11)

在此先感謝您的幫助!

Grüße!

+0

不適合我。十進制是用於轉換的函數: – 2014-09-02 16:41:36

+0

您是否嘗試過不使用「小數」運行相同的查詢,並通過放置一些測試值(如「1.01」)? – Vor 2014-09-02 16:41:45

+0

從十進制* 然後可以使用十進制(xxx) – 2014-09-02 16:42:21

回答

1

試試這個,而不是

cursor.execute("UPDATE einzelanalyse.analysen SET Kurs bei empf = %s WHERE analysen.Nummer = %s"% (Decimal(kurs),i[12])) 

你缺少你需要的不過字符串格式化額外%建議使用mysql格式

cursor.execute("UPDATE einzelanalyse.analysen SET Kurs bei empf = %s WHERE analysen.Nummer = %s", (Decimal(kurs),i[12])) 
+1

完全相同。這從來沒有發生過我。我給了你一個+1 – bernie 2014-09-02 16:46:16

+0

太棒了!有用!它越隱蔽,越有可能在某個地方出現錯誤......感謝大家的支持!我們綁: – 2014-09-02 17:24:49

1

的錯誤是,你錯過了UPDATE語句和元組之間的逗號。請將代碼更改爲:

cursor.execute("""UPDATE einzelanalyse.analysen 
        SET Kurs bei empf = %s 
        WHERE analysen.Nummer = %s""", (Decimal(kurs),i[12])) 
               #^this is where the comma is necessary 

以前發生的事情是您有一個字符串,例如, 「abc」然後在它後面的括號/括號,例如「abc」(...)看起來像是在試圖調用一個字符串。因此,錯誤TypeError: 'str' object is not callable是有意義的。

+1

:P +1好回答:) – 2014-09-02 16:46:09

0
#Create strings for values 
empf = Decimal(kurs) 
analysen = i[12] 

#python notation for placeholders should be noted as {0} {1} etc. instead of %s 
#use .format(string,string) to point back to {0} and {1} 
# Create a query string 
query = "update einzelanalyse.analysen set kurs bei empf = {0} where analysen.nummer {1}".format(empf,analysen) 

# Let the cursor just run the prebuild query string 
cursor.execute(query)