Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #36125 > unrolled thread
| Started by | andydtaylor@gmail.com |
|---|---|
| First post | 2013-01-04 10:08 -0800 |
| Last post | 2013-01-05 04:24 -0800 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
Evaluate postgres boolean field andydtaylor@gmail.com - 2013-01-04 10:08 -0800
Re: Evaluate postgres boolean field donarb <donarb@nwlink.com> - 2013-01-04 10:50 -0800
Re: Evaluate postgres boolean field John Gordon <gordon@panix.com> - 2013-01-04 19:06 +0000
Re: Evaluate postgres boolean field andydtaylor@gmail.com - 2013-01-05 04:24 -0800
| From | andydtaylor@gmail.com |
|---|---|
| Date | 2013-01-04 10:08 -0800 |
| Subject | Evaluate postgres boolean field |
| Message-ID | <d559ab56-241b-49d0-84fd-ebd0b70426ab@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | donarb <donarb@nwlink.com> |
|---|---|
| Date | 2013-01-04 10:50 -0800 |
| Message-ID | <9af870c7-6afb-40a9-becb-238f0e52a69d@googlegroups.com> |
| In reply to | #36125 |
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 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
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']
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-01-04 19:06 +0000 |
| Message-ID | <kc798c$79a$1@reader1.panix.com> |
| In reply to | #36125 |
In <d559ab56-241b-49d0-84fd-ebd0b70426ab@googlegroups.com> andydtaylor@gmail.com 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
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | andydtaylor@gmail.com |
|---|---|
| Date | 2013-01-05 04:24 -0800 |
| Message-ID | <5bc46274-c0ef-4d4b-9a44-87ef5ce468c0@googlegroups.com> |
| In reply to | #36135 |
Brilliant, thanks guys
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web