TypeError:并非在字符串格式化postgres期间转换所有参数

时间:2016-07-24 14:40:02

标签: python postgresql psycopg2

我尝试将数据添加到数据库中(使用psycopg2.connect):

cand = Candidate('test', datetime.now(), 'test@test.t', '123123', "21", 'test', 'test', 'test', datetime.now(), "1", "1", 'test', 'M', "18", "2", "2")
db.addCandidate(cand)

我的功能添加:

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工作正常

3 个答案:

答案 0 :(得分:0)

我无法确定,因为我不知道您的列的类型,但是您的日期时间对象的格式不合适似乎是合理的。您发送的日期时间对象不是正确日期格式的字符串。尝试:

candidate.pub_date.isoformat()

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

有关格式选项,请参阅http://strftime.org/。这可能对你有用。

答案 1 :(得分:0)

而不是execute执行mogrify来检查发送到服务器的确切内容:

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
    )
)

答案 2 :(得分:0)

我解决了我的问题,我写了15'%s'而不是16