类型错误:在未格式化字符串转换Postgres的所有参数
问题描述:
我尝试将数据添加到数据库中(使用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工作正常
答
我解决我的问题,我写了15“%s”的,而不是16
答
我不能确定,因为我不知道你的列的类型,但它是可能的,你的datetime对象是不正确的格式。您在日期时间对象中发送的日期格式不是正确的字符串。尝试:
candidate.pub_date.isoformat()
或
candidate.pub_date.strftime("<proper_format">)
的格式选项见http://strftime.org/。这可能对你有用。
+0
谢谢,我试过了,我甚至试图在声明中删除完整的日期,错误 – Vladimir
答
在代替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
)
)
什么框架或库是否用来连接到PostgreSQL数据库?你是否收到任何错误信息或警告? “什么都没有改变”是什么意思?数据库内容是否保持不变?请尝试提供[mcve]。 –
mysql和postgres的行为不一样。也许你的一个价值观不是一个字符串?在执行之前打印出你的sql语句的结果。 –
你需要引用datetime()结果吗? –