TypeError: not all arguments converted during string formatting postgres

TypeError: not all arguments converted during string formatting postgres

我尝试向数据库中添加数据(使用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 工作正常

我不能确定,因为我不知道你的列的类型,但你的 datetime 对象的格式可能不正确。您发送的日期时间对象不是正确日期格式的字符串。尝试:

candidate.pub_date.isoformat()

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

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

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

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