Am 23.08.2016 um 06:57 schrieb raf:
Here's a little program to demonstrate:

  #!/usr/bin/env python
  import pgdb
  connection = pgdb.connect(host='XXX', database='XXX', user='XXX', 
password='XXX')
  cursor = connection.cursor()
  cursor.execute("select cast('[2:2]={1}' as integer[])")
  rows = cursor.fetchall()
  print('%r' % rows[0][0])
  # pygresql-4.2.2 outputs '[2:2]={1}' (which then needs to be parsed)
  # pygresql-5.0.1 outputs [1]         (which should be [None, 1])

Why do you think this should be [None, 1]?

The expression "[2:2]={1}" actually describes an array with ONE element, "1". This translates into a list with ONE element in Python.

The "[2:2]=" prefix only says that Postgres should use the start and end index 2 for that index, but that's only for accessing the array internally in Postgres and does not translate to Python, since Python lists always have the start index 0. Note that using this prefix you can only change the index, not magically add NULL elements.

  (cast('[2:2]={1}' as integer[]))[2]  --> 1
  array_length(cast('[2:2]={1}' as integer[]), 1)  --> 1

As you see, the actual array has still only one item even in Postgres, regardless of the fact that the start index is set to 2. So it should be also translated to a Python list with only one item.

However, if you really want a Null element as part of the list, you can do it as follows:

  cursor.execute("select cast('{NULL,1}' as integer[])")
  rows = cursor.fetchall()
  print('%r' % rows[0][0])

This should in fact output [None, 1].

-- Chris (back from vacation)
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to