[Tutor] postgreSQL + psycopg2

nitin chandra nitinchandra1 at gmail.com
Tue May 10 01:29:37 EDT 2016


Thanks Alan, Peter

but didn't work

    nextrow=(cursor1.execute(query, (design1,))).fetchone()
AttributeError: 'NoneType' object has no attribute 'fetchone'

1. Yes Alan, the variable 'design1' has a value '1'.

This is how I tested it from python command :

>>> import psycopg2
>>> import datetime
>>> line = (1, 'Vinayak', 'Salunke', '1', datetime.date(1982, 6, 6), 9871234567L, 'Tower-1,Millinium tower,Gurgaon,India', 0, 1, datetime.date(2016, 5, 6), datetime.datetime(2016, 5, 6, 22, 55, 5, tzinfo=psycopg2.tz.FixedOffsetTimezone(offset=330, name=None)))
>>> design1 = line[3]
>>> print design1
1
>>> conn = psycopg2.connect(database="passtms", user="nitin", host="localhost", password="")
>>> cursor1 = conn.cursor()
>>> query=("""SELECT designation_name FROM designation WHERE designation_id = %s;""")
>>> nextrow = (query, (design1))
>>> nextrow
('SELECT designation_name FROM designation WHERE designation_id = %s;', '1')
>>> nextrow = cursor1.execute(query, (design1,))
>>> nextrow
>>>

When we run "cursor1.execute", that is when it gives us None.

How strange !!??

On 10 May 2016 at 04:36, Alan Gauld via Tutor <tutor at python.org> wrote:
> On 09/05/16 22:14, nitin chandra wrote:
>
>> for line1 in smallLIST1:
>>     design1 = line1[3]
>>     print design1
>>     nextRow=cursor1.execute("SELECT designation_name FROM designation
>> WHERE designation_id = %s;", (design1))
>>     print nextRow
>>     print """<tr>"""
>>     for row in line1:
>>         print """<td>"""+str(row)+"""</td>"""
>>     print """</tr>"""
>>
>> 1st "print" shows values 1 , correctly.
>
> I assume you mean design1?
>
>> 2nd "print" shows "None".
>
> I don't know postgres API so i'll assume the %s is  the correct
> marker. In SQLite we'd use ?...
>
> Also to get the results row by row I'd expect to have
> to call fetchone() on the cursor:
>
> query = """
> SELECT designation_name
> FROM designation
> WHERE designation_id = %s;"""
>
> nextrow = cursor1.execute(query, (design1,)).fetchone()
>
> But Postgres DBAPI may be different...
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list