添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeing this on Windows, under jupyter/ anaconda. I'm guessing execute_values is intended for inserting more than one element per row, but that's not explicit in the documentation.

psycopg version 2.7.1.

print(sys.version)
3.6.0 |Anaconda 4.3.0 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)]
import psycopg2
import psycopg2.extras
conn = psycopg2.connect("[connect_string]")
cur = conn.cursor()
values1 = ["richard harrison", "george peterson"] 
query="""insert into testinserts (testvar) values %s"""
psycopg2.extras.execute_values(cur, query, values1)
C:\Users\x\Anaconda3\lib\site-packages\psycopg2\extras.py in execute_values(cur, sql, argslist, template, page_size)
   1245         parts = pre[:]
   1246         for args in page:
-> 1247             parts.append(cur.mogrify(template, args))
   1248             parts.append(b',')
   1249         parts[-1:] = post
TypeError: 'int' object does not support indexing

Insert works with this, though, when the query includes 2 column names:
values1 = [('richard harrison', 1),('george peterson', 2)]

List of ints generate a different error:

values2 = [1, 2, 3, 4, 5]
psycopg2.extras.execute_values(cur, query, values2)
C:\Users\x\Anaconda3\lib\site-packages\psycopg2\extras.py in execute_values(cur, sql, argslist, template, page_size)
   1242     for page in _paginate(argslist, page_size=page_size):
   1243         if template is None:
-> 1244             template = b'(' + b','.join([b'%s'] * len(page[0])) + b')'
   1245         parts = pre[:]
   1246         for args in page:
TypeError: object of type 'int' has no len()
          

Yes, it is explicit: from the docs args:

argslist – sequence of sequences or dictionaries with the arguments to send to the query.

a list of integers is not a sequence of sequences (a list of string is... but it's wrong). You can use a list of 1-item tuples or 1-item lists instead. Try with:

values1 = [("richard harrison",), ("george peterson",)] 
values2 = [(i,) for i in range(10)]

ecc. If it doesn't work, reopen the bug.