如何修复psycopg2中的语法错误,出现在'%'附近?

huangapple go评论158阅读模式
英文:

How to fix the psycopg2 syntax error at or near '%'?

问题

基本上是在尝试创建一个INSERT函数,该函数将编译插入操作并执行它。

  1. def Insert(self, tablename, **kwargs):
  2. qms = []
  3. for idx, item in enumerate(kwargs):
  4. qms.append('%%s')
  5. qmarks = ', '.join(qms)
  6. res = []
  7. res.extend(kwargs.keys())
  8. res.extend(kwargs.values())
  9. res = tuple(res)
  10. QUERY = "INSERT INTO " + tablename + " (%s) VALUES (%s)" % (qmarks, qmarks)
  11. self.dc.execute(QUERY, res)
  12. self.db.commit()

返回错误信息如下:

  1. psycopg2.errors.SyntaxError: syntax error at or near "%"
  2. LINE 1: INSERT INTO users (%s, %s) VALUES (%s, %s)
英文:

Basically trying to create ISNERT function, which will compile the insert and execute it.

  1. def Insert(self,tablename, **kwargs):
  2. qms = []
  3. for idx, item in enumerate(kwargs):
  4. qms.append('%%s')
  5. qmarks = ', '.join(qms)
  6. res = []
  7. res.extend(kwargs.keys())
  8. res.extend(kwargs.values())
  9. res = tuple(res)
  10. QUERY = "INSERT INTO "+tablename+" (%s) VALUES (%s)" % (qmarks, qmarks)
  11. self.dc.execute(QUERY, res)
  12. self.db.commit()

Returns me:

  1. psycopg2.errors.SyntaxError: syntax error at or near "%"
  2. LINE 1: INSERT INTO users (%s, %s) VALUES (%s, %s)

答案1

得分: 2

psycopg2的execute()函数只会注入值,而不会注入列名,你需要将真实的列名传递给QUERY:

  1. import psycopg2
  2. import psycopg2.extras
  3. class A():
  4. def __init__(self):
  5. self.dc = ...
  6. self.db = ...
  7. def Insert(self, tablename, **kwargs):
  8. columns = []
  9. valuePlaceholders = []
  10. values = []
  11. for column, value in kwargs.items():
  12. columns.append(column)
  13. valuePlaceholders.append('%%s')
  14. values.append(value)
  15. QUERY = "INSERT INTO " + tablename + " (%s) VALUES (%s)" % (', '.join(columns), ', '.join(valuePlaceholders))
  16. print(self.db.mogrify(QUERY, values))
  17. #self.db.execute(QUERY, values)
  18. #self.db.commit()
  19. a = A().Insert('foo', arg1="bar", arg2="test")

输出:

  1. b'INSERT INTO foo (arg1, arg2) VALUES (%s, %s)'
英文:

psycopg2's execute() function just injects the values but not the columns, you need to pass the real column names to QUERY:

  1. import psycopg2
  2. import psycopg2.extras
  3. class A():
  4. def __init__(self):
  5. self.dc = ...
  6. self.db = ...
  7. def Insert(self, tablename, **kwargs):
  8. columns = []
  9. valuePlaceholders = []
  10. values = []
  11. for column, value in kwargs.items():
  12. columns.append(column)
  13. valuePlaceholders.append('%%s')
  14. values.append(value)
  15. QUERY = "INSERT INTO " + tablename + " (%s) VALUES (%s)" % (', '.join(columns), ', '.join(valuePlaceholders))
  16. print(self.db.mogrify(QUERY, values))
  17. #self.db.execute(QUERY, values)
  18. #self.db.commit()
  19. a = A().Insert('foo', arg1="bar", arg2="test")

Output:

  1. b'INSERT INTO foo (arg1, arg2) VALUES (%s, %s)'

答案2

得分: 0

你的qms数组被填充了字符串值'%s'。
然后,你的qmarks字符串等于'%s, %s'。
接下来,你将该值注入到你的查询字符串中两次,所以你得到'INSERT INTO users (%s, %s) VALUES (%s, %s)'。
问题在于你已经用不正确的值填充了你的数组。
你需要注入表名和值,而不是%s。

英文:

Your qms array is filled with the string values '%s'.
Your qmarks string then equals '%s, %s'.
After that you inject that value in your query string twice so you have 'INSERT INTO users (%s, %s) VALUES (%s, %s)'.
The issue here is that you have filled your array with incorrect values.
You need to inject your table names and values instead of %s

huangapple
  • 本文由 发表于 2020年1月6日 18:16:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定