Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Evaluate postgres boolean field

Reply
Thread Tools

Evaluate postgres boolean field

 
 
andydtaylor@gmail.com
Guest
Posts: n/a
 
      01-04-2013
Hi,

I'm hoping for some help on a python script I need to query an api. I'm not a (Python) programmer ordinarily, but do plan to improve!

Specifically I have a for loop evaluating a database row, which I think I can treat as a list. My [4] is a postgres boolean field, and I'm temporarily stuck on how to evaluate this to determine if I use the values in [1].

Could I have some advice on what to change? Also do let me know if you can recommend a good beginners python book.

Data example:

[13, 'Barbican Station', 'Barbican Station, London Underground Ltd., Aldersgate St, London, EC1A 4JA', '01010000E0E61000008851AB9E9803B9BF5BB6972294C2494 000000000000000000000000000000000', True]


Code:

#!/usr/bin/python
import psycopg2
#note that we have to import the Psycopg2 extras library!
import psycopg2.extras
import sys

def main():
conn_string = "host='localhost' dbname='gisdb' user='postgres' password='#########'"
# print the connection string we will use to connect
print "Connecting to database\n ->%s" % (conn_string)

conn = psycopg2.connect(conn_string)

# HERE IS THE IMPORTANT PART, by specifying a name for the cursor
# psycopg2 creates a server-side cursor, which prevents all of the
# records from being downloaded at once from the server.
cursor = conn.cursor('cursor_tube', cursor_factory=psycopg2.extras.DictCursor)
cursor.execute('SELECT * FROM tubestations LIMIT 1000')

# Because cursor objects are iterable we can just call 'for - in' on
# the cursor object and the cursor will automatically advance itself
# each iteration.
# This loop should run 1000 times, assuming there are at least 1000
# records in 'my_table'
row_count = 0
for row in cursor:
row_count += 1
if row[4] = True
print row[1]
#print "row: %s %s\n" % (row_count, row)

if __name__ == "__main__":
main()



Thanks!


Andy
 
Reply With Quote
 
 
 
 
donarb
Guest
Posts: n/a
 
      01-04-2013
On Friday, January 4, 2013 10:08:22 AM UTC-8, andyd...@gmail.com wrote:
> Hi,
>
> I'm hoping for some help on a python script I need to query an api. I'm not a (Python) programmer ordinarily, but do plan to improve!
>
> Specifically I have a for loop evaluating a database row, which I think Ican treat as a list. My [4] is a postgres boolean field, and I'm temporarily stuck on how to evaluate this to determine if I use the values in [1].
>
> Could I have some advice on what to change? Also do let me know if you can recommend a good beginners python book.
>
> Data example:
>
> [13, 'Barbican Station', 'Barbican Station, London Underground Ltd., Aldersgate St, London, EC1A 4JA', '01010000E0E61000008851AB9E9803B9BF5BB6972294C2494 000000000000000000000000000000000', True]
>
>
> Code:
>
> #!/usr/bin/python
> import psycopg2
> #note that we have to import the Psycopg2 extras library!
> import psycopg2.extras
> import sys
>
> def main():
> conn_string = "host='localhost' dbname='gisdb' user='postgres' password='#########'"
> # print the connection string we will use to connect
> print "Connecting to database\n ->%s" % (conn_string)
>
> conn = psycopg2.connect(conn_string)
>
> # HERE IS THE IMPORTANT PART, by specifying a name for the cursor
> # psycopg2 creates a server-side cursor, which prevents all of the
> # records from being downloaded at once from the server.
> cursor = conn.cursor('cursor_tube', cursor_factory=psycopg2.extras.DictCursor)
> cursor.execute('SELECT * FROM tubestations LIMIT 1000')
>
> # Because cursor objects are iterable we can just call 'for - in' on
> # the cursor object and the cursor will automatically advance itself
> # each iteration.
> # This loop should run 1000 times, assuming there are at least 1000
> # records in 'my_table'
> row_count = 0
> for row in cursor:
> row_count += 1
> if row[4] = True
> print row[1]
> #print "row: %s %s\n" % (row_count, row)
>
> if __name__ == "__main__":
> main()
>
> Thanks!
>
>
> Andy


Your code is pretty close to working, you just need to make a couple modifications. You are using the equals sign as an assignment, not a comparison, although the comparison and value are unnecessary since the field's value is either true or false. And you're missing a colon at the end of the condition. Note also that since you are using a DictCursor you can use column names to reference your row's fields, I guessed on the field names, but you should get the idea.


for row in cursor:
row_count += 1
if row['active']:
print row['name']
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      01-04-2013
In <d559ab56-241b-49d0-84fd-> writes:

> for row in cursor:
> row_count += 1
> if row[4] = True
> print row[1]


Since row[4] is a boolean value, you should be able to just say:

if row[4]:
print row[1]

--
John Gordon A is for Amy, who fell down the stairs
B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
andydtaylor@gmail.com
Guest
Posts: n/a
 
      01-05-2013
Brilliant, thanks guys
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Subtle difference between boolean value and boolean comparison? Metre Meter Javascript 7 08-06-2010 08:40 PM
Best way to evaluate boolean expressions from strings? Gustavo Narea Python 4 04-28-2009 08:09 PM
difference between 'boolean' and 'java.lang.Boolean' J Leonard Java 4 01-19-2008 02:56 AM
Make a class evaluate to false in boolean expressions Matt Margolis Ruby 7 10-09-2007 09:19 PM
Postgres/Postgres-pr - some confusion Nick Black Ruby 1 11-16-2006 08:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57