2016-07-24 93 views
0

我嘗試將數據添加到數據庫中(使用psycopg2.connect):類型錯誤:在未格式化字符串轉換Postgres的所有參數

cand = Candidate('test', datetime.now(), '[email protected]', '123123', "21", 'test', 'test', 'test', datetime.now(), "1", "1", 'test', 'M', "18", "2", "2") 
db.addCandidate(cand) 

我函數add:

def addCandidate(self, candidate): 
    with self.connection.cursor() as cursor: 
     cursor.execute("""INSERT INTO candidate (name, pub_date, email, tel, age, proff, href, city, last_update, called_count, status, comment, sex, recrut_id, vacancy_id, level) 
       VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""", (candidate.name, candidate.pub_date, candidate.email, candidate.tel, 
          candidate.age, candidate.proff, candidate.href, candidate.city, candidate.last_update, candidate.called_count, candidate.status, candidate.comment, candidate.sex, candidate.recrut_id, 
          candidate.vacancy_id, candidate.level)) 
     self.connection.commit() 

試圖包裝數據str,但沒有任何改變。 在pymysql.connect工作正常

+0

什麼框架或庫是否用來連接到PostgreSQL數據庫?你是否收到任何錯誤信息或警告? 「什麼都沒有改變」是什麼意思?數據庫內容是否保持不變?請嘗試提供[mcve]。 –

+0

mysql和postgres的行爲不一樣。也許你的一個價值觀不是一個字符串?在執行之前打印出你的sql語句的結果。 –

+2

你需要引用datetime()結果嗎? –

回答

0

我解決我的問題,我寫了15「%s」的,而不是16

0

我不能確定,因爲我不知道你的列的類型,但它是可能的,你的datetime對象是不正確的格式。您在日期時間對象中發送的日期格式不是正確的字符串。嘗試:

candidate.pub_date.isoformat() 

candidate.pub_date.strftime("<proper_format">) 

的格式選項見http://strftime.org/。這可能對你有用。

+0

謝謝,我試過了,我甚至試圖在聲明中刪除完整的日期,錯誤 – Vladimir

0

在代替executemogrify檢查到底是被髮送到服務器的內容:

print cursor.mogrify (""" 
    INSERT INTO candidate ( 
     name, pub_date, email, tel, age, proff, href, city, 
     last_update, called_count, status, comment, 
     sex, recrut_id, vacancy_id, level 
    ) VALUES (
     %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s 
    )""", (
     candidate.name, candidate.pub_date, candidate.email, 
     candidate.tel, candidate.age, candidate.proff, candidate.href, 
     candidate.city, candidate.last_update, candidate.called_count, 
     candidate.status, candidate.comment, candidate.sex, 
     candidate.recrut_id, candidate.vacancy_id, candidate.level 
    ) 
) 
相關問題