Evaluate postgres boolean field

Discussion in 'Python' started by andydtaylor@gmail.com, Jan 4, 2013.

  1. Guest

    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', '01010000E0E61000008851AB9E9803B9BF5BB6972294C2494000000000000000000000000000000000', 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
    , Jan 4, 2013
    #1
    1. Advertising

  2. donarb Guest

    On Friday, January 4, 2013 10:08:22 AM UTC-8, 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', '01010000E0E61000008851AB9E9803B9BF5BB6972294C2494000000000000000000000000000000000', 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']
    donarb, Jan 4, 2013
    #2
    1. Advertising

  3. John Gordon Guest

    In <> 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"
    John Gordon, Jan 4, 2013
    #3
  4. Guest

    Brilliant, thanks guys
    , Jan 5, 2013
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J Leonard
    Replies:
    4
    Views:
    12,671
    Mark Space
    Jan 19, 2008
  2. Gustavo Narea
    Replies:
    4
    Views:
    1,891
    Arnaud Delobelle
    Apr 28, 2009
  3. Nick Black
    Replies:
    1
    Views:
    153
    Kashia Buch
    Nov 16, 2006
  4. Matt Margolis
    Replies:
    7
    Views:
    107
    Robert Klemme
    Oct 9, 2007
  5. Metre Meter
    Replies:
    7
    Views:
    366
    Metre Meter
    Aug 6, 2010
Loading...

Share This Page