【发布时间】:2015-06-18 14:40:37
【问题描述】:
我正在使用 python Flask,但我无法理解为什么会出错:
ProgrammingError: syntax error at or near "IF"
LINE 1: IF SELECT count(*) FROM ProfilePicture WHERE userid =
这是我的代码:
> def updateProfilePicture(filename, image, userid):
> cursor = getCursor()
> binary = psycopg2.Binary(image)
> data = (userid, filename, binary, userid, filename, binary, userid)
> #SQL = """INSERT INTO ProfilePicture(id, image, userid)
> # VALUES (%s, %s, %s)"""
> SQL = """IF SELECT count(*) FROM ProfilePicture WHERE userid = %s > 0
> THEN
> UPDATE ProfilePicture SET id = %s, image = %s WHERE userid = %s
> ELSE
> INSERT INTO ProfilePicture(id, image, userid) VALUES (%s, %s, %s)
> END IF"""
> print cursor.mogrify(SQL, data)
> cursor.execute(SQL, data)
> cursor.connection.commit()
> cursor.close()
> return
一个简单的插入效果很好,但不是 if 语句。
感谢您的帮助!
【问题讨论】:
-
我认为PostgreSQL中没有这样的语法
-
Insert, on duplicate update in PostgreSQL? 看起来可能相关/适用。
-
更好用
if exists (select * from ...)
标签: python sql postgresql flask