Re: Psycopg2 package installation puzzle in Pycharm - any thoughts?

2014-09-01 Thread andydtaylor
Posting style point taken. Google groups doesn't exactly help you with that.

* You guys have probably been tinkering with this stuff for years. I haven't.  
* Your man on the street would say I described the error fairly well. 
* It's not like I wasn't trying my best to fix it myself
* So far as environment tinkering is concerned I have entered a launchctl 
setenv PATH statement in two places. Both reversible.
* I think what I have is an issue pertaining to OSX Mavericks and Pycharm. If 
you have nothing to add, just say it. 
* Statements like Please equip yourself with a tool that provides us with some 
context add nothing and are not exactly community inclusive.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Psycopg2 package installation puzzle in Pycharm - any thoughts?

2014-09-01 Thread andydtaylor
Mark - it's more that I just didn't understand what you mean. 

Here's you: Probably an out and out programmer; uses a number of languages; a 
decade of experience, educated in best practice via your experience.

Here's me: Idiot. A decade of experience in VBA and Excel in mindless finance 
jobs, hoping to build a 'not shit website for a refrigerated van courier 
company I am starting.  I know enough to find my way around linux and mac for 
most things and that Python and Django are a great tool, yet I stumble in my 
environment setup. I have built a Django website before using Pycharm on an 
Ubuntu machine without this problem.

So I wasn't seeking to be abrasive, it's just that I don't get it, nor do I 
understand the boundary of it.

Here's what I can do:

1. I will lookup Thunderbird and see if this makes posting easier to follow
2. I will reverse my launchctl setenv changes.
3. Is there anything else I can do to improve diagnosis and communication?

Andy
-- 
https://mail.python.org/mailman/listinfo/python-list


Psycopg2 package installation puzzle in Pycharm - any thoughts?

2014-08-31 Thread andydtaylor
Hi,

I have a puzzle, for which I would appreciate your opinion on!

I have been trying to setup a project in Pycharm with psycopg2.

If I install it using pip install it is added. However  if I use the Pycharm 
preferencesproject interpreteradd package option it fails with the below 
message. 


Please add the directory containing pg_config to the PATH

or specify the full executable path with the option:

python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.


Cleaning up...
Command python setup.py egg_info failed with error code 1 in 
/private/var/folders/79/wxtpjs7x3wx_v01fz8h5y8pwgp/T/pycharm-packaging4753300326598484086.tmp/psycopg2
Storing debug log for failure in /Users/admin/.pip/pip.log

in .bash_profile I have the following:

export PATH=$PATH:/usr/local/share/python
export PATH=$PATH:/usr/local/bin
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
export PYTHONPATH=$PYTHONPATH:/usr/local/bin/python

export WORKON_HOME=$HOME/.Virtualenvs
export PROJECT_HOME=$HOME/Projects
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
# source /usr/local/share/python/virtualenvwrapper.sh

alias showhidden='defaults write com.apple.finder AppleShowAllFiles TRUE; 
killall Finder'
alias hidehidden='defaults write com.apple.finder AppleShowAllFiles FALSE; 
killall Finder


Notes
-Mac OS
-Python and Postgres installed using brew
-Django project
- Installing to a virtualenv python environment.

Let me know if you have an idea how I can fix this!

Thanks,

Andy
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Psycopg2 package installation puzzle in Pycharm - any thoughts?

2014-08-31 Thread andydtaylor
Actually I realise that postgres app didn't come from brew I wonder if 
that's the issue

On Sunday, 31 August 2014 15:19:24 UTC+1, andyd...@gmail.com  wrote:
 Hi,
 
 
 
 I have a puzzle, for which I would appreciate your opinion on!
 
 
 
 I have been trying to setup a project in Pycharm with psycopg2.
 
 
 
 If I install it using pip install it is added. However  if I use the Pycharm 
 preferencesproject interpreteradd package option it fails with the below 
 message. 
 
 
 
 
 
 Please add the directory containing pg_config to the PATH
 
 
 
 or specify the full executable path with the option:
 
 
 
 python setup.py build_ext --pg-config /path/to/pg_config build ...
 
 
 
 or with the pg_config option in 'setup.cfg'.
 
 
 
 
 
 Cleaning up...
 
 Command python setup.py egg_info failed with error code 1 in 
 /private/var/folders/79/wxtpjs7x3wx_v01fz8h5y8pwgp/T/pycharm-packaging4753300326598484086.tmp/psycopg2
 
 Storing debug log for failure in /Users/admin/.pip/pip.log
 
 
 
 in .bash_profile I have the following:
 
 
 
 export PATH=$PATH:/usr/local/share/python
 
 export PATH=$PATH:/usr/local/bin
 
 export 
 PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin/pg_config
 
 export PYTHONPATH=$PYTHONPATH:/usr/local/bin/python
 
 
 
 export WORKON_HOME=$HOME/.Virtualenvs
 
 export PROJECT_HOME=$HOME/Projects
 
 export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python
 
 export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
 
 source /usr/local/bin/virtualenvwrapper.sh
 
 # source /usr/local/share/python/virtualenvwrapper.sh
 
 
 
 alias showhidden='defaults write com.apple.finder AppleShowAllFiles TRUE; 
 killall Finder'
 
 alias hidehidden='defaults write com.apple.finder AppleShowAllFiles FALSE; 
 killall Finder
 
 
 
 
 
 Notes
 
 -Mac OS
 
 -Python and Postgres installed using brew
 
 -Django project
 
 - Installing to a virtualenv python environment.
 
 
 
 Let me know if you have an idea how I can fix this!
 
 
 
 Thanks,
 
 
 
 Andy

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Psycopg2 package installation puzzle in Pycharm - any thoughts?

2014-08-31 Thread andydtaylor
Chris-

I have removed the second copy of postgres I had (postgres.app) and updated 
path variables in .bash_profile:

export PATH=$PATH:/usr/local/share/python
export PATH=$PATH:/usr/local/bin
export PYTHONPATH=$PYTHONPATH:/usr/local/bin/python
export PATH=$PATH:/usr/local/Cellar/postgresql/9.3.5_1/bin

I don't think I should need the last line because /usr/local/bin/pg_config 
exists as an alias for /usr/local/Cellar/postgresql/9.3.5_1/bin/pgconfig - do 
you think I am right?

I have read that window apps may be unable to pick up environment variables. 
hence I added the below to the end of my .bash_profile

launchctl setenv PATH $PATH

But still Pycharm wasn't having it.  So I have created /etc/launchd.conf and 
put this in it:

launchctl setenv PATH 
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/python:/usr/local/bin

Now I am out of ideas. It's quite maddening!

Any advice appreciated.

Thanks,

Andy





-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Psycopg2 package installation puzzle in Pycharm - any thoughts?

2014-08-31 Thread andydtaylor
Chris- 

I have removed the second copy of postgres I had (postgres.app) and updated 
path variables in .bash_profile: 

export PATH=$PATH:/usr/local/share/python 
export PATH=$PATH:/usr/local/bin 
export PYTHONPATH=$PYTHONPATH:/usr/local/bin/python 
export PATH=$PATH:/usr/local/Cellar/postgresql/9.3.5_1/bin 

I don't think I should need the last line because /usr/local/bin/pg_config 
exists as an alias for /usr/local/Cellar/postgresql/9.3.5_1/bin/pgconfig - do 
you think I am right? 

I have read that window apps may be unable to pick up environment variables. 
hence I added the below to the end of my .bash_profile 

launchctl setenv PATH $PATH 

But still Pycharm wasn't having it.  So I have created /etc/launchd.conf and 
put this in it: 

launchctl setenv PATH 
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/python:/usr/local/bin
 

Andrea - yes I am using the virtualenv interpreter as the Pycharm project 
interpreter

Now I am out of ideas. It's quite maddening! 

Any advice appreciated. 

Thanks, 

Andy 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Psycopg2 package installation puzzle in Pycharm - any thoughts?

2014-08-31 Thread andydtaylor
FYI My mac version is Mavericks 10.9.4
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Psycopg2 package installation puzzle in Pycharm - any thoughts?

2014-08-31 Thread andydtaylor
I have gone ahead and set it all up by using pip install psycopg2 but I would 
still like to determine why Pycharm couldn't find the $PATH variable
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best way to convert number of minutes to hh:mm AM/PM?

2013-03-02 Thread andydtaylor
Brilliant, thanks to all
-- 
http://mail.python.org/mailman/listinfo/python-list


Best way to convert number of minutes to hh:mm AM/PM?

2013-03-01 Thread andydtaylor
Hi,

I need to create a list of equally spaced times (as in hh:mm AM/PM) within a 
day to loop through. Having selected 30 minute intervals I figured I could:

* Create a list from 1 to 48
* Multiply each value by 30
* Convert minutes to a time. datetime.timedelta seems to do this, but it's not 
a full timestamp which means strftime can't format me a time with am/pm.

can anyone suggest a good approach to use? Ultimately I'd like to generate an 
equivalent to this text/format:'2:30 pm'

Thanks,


Andy
-- 
http://mail.python.org/mailman/listinfo/python-list


Escaping list of numbers for Postgres column names

2013-02-23 Thread andydtaylor
Hi

I have some convenient short place name IDs which would be handy for column 
names. Unfortunately, many begin with a number. 

I can work around this by appending a letter to each one, but should I escape 
the number in such a way that I can use it directly as my column name, in the 
same way as I might an apostrophe? How could I do that? I wondered what the 
best approach is to take.


My python list come out looking  like this:

 print stn_list_short
['9400ZZLUACY1', '49238006', '49276005', '9400ZZLUACT2']

I'm producing it using this code:

 for row in cursor_from:
... if row[8]:
... # Station values for database
... stn_list_short.append(row[0])

And trying to employ the list something like this:

 create_table_fields = '(id serial PRIMARY KEY, station_code varchar, %s 
 varchar)' % ' varchar, '.join(map(str, \ + str(stn_list_short)))


Thanks,


Andy

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Escaping list of numbers for Postgres column names

2013-02-23 Thread andydtaylor
In reply to my own question, postgres column names must begin with a letter or 
an underscore. So this is what I have done:

 for row in cursor_from:
... if row[8]:
... stn_list_short.append(_ + row[0])

I can now use stn_list_short to create my columns
-- 
http://mail.python.org/mailman/listinfo/python-list


psycopg2 craziness

2013-02-21 Thread andydtaylor


Hi,

I'm trying to use psycopg2 but having some issues. Would it be possible to get 
some pointers? I seem unable to execute any SQL statements.


So here are my database tables before (and after) I try to do anything:

postgres=# \c
You are now connected to database postgres as user postgres.
postgres=# \d
List of relations
 Schema |  Name   |   Type   |   Owner
+-+--+
 public | loc_all | table| django_dev
 public | loc_all_unique  | table| django_dev
 public | loc_dlr | table| django_dev
 public | loc_londonbuses | table| django_dev
 public | loc_londonriverservices | table| django_dev
 public | loc_londonunderground   | table| django_dev
 public | loc_tramlink| table| django_dev
 public | lu_stations_id_seq  | sequence | postgres
 public | postcode_input  | table| postgres
 public | postcode_lat_long   | table| django_dev
 public | test_foo| table| django_dev
 public | tubecross   | table| django_dev


Here's what I've been trying to execute in the python shell:

 import psycopg2
 import psycopg2.extras
 db = psycopg2.connect(
... host = 'localhost',
... database = 'postgres',
... user = 'postgres',
... password = 'password'
... )
 cursor_to = db.cursor()
 cursor_to.execute(CREATE TABLE foo (id serial PRIMARY KEY);)
 cursor_to.execute(CREATE TABLE foo (id serial PRIMARY KEY);)
Traceback (most recent call last):
  File stdin, line 1, in module
psycopg2.ProgrammingError: relation foo already exists

 
 
 cursor_to.execute(CREATE TABLE foo (id serial PRIMARY KEY);)
Traceback (most recent call last):
  File stdin, line 1, in module
psycopg2.InternalError: current transaction is aborted, commands ignored until 
end of transaction block


Here's the postgres log:

2013-02-21 22:22:20 GMT LOG:  database system was shut down at 2013-02-21 
22:21:41 GMT
2013-02-21 22:22:20 GMT LOG:  autovacuum launcher started
2013-02-21 22:22:20 GMT LOG:  database system is ready to accept connections
2013-02-21 22:22:20 GMT LOG:  incomplete startup packet
2013-02-21 22:22:21 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:22 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:22 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:23 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:23 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:24 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:24 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:25 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:25 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:26 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:26 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 22:22:26 GMT LOG:  incomplete startup packet
2013-02-21 22:28:52 GMT ERROR:  relation foo already exists
2013-02-21 22:28:52 GMT STATEMENT:  CREATE TABLE foo (id serial PRIMARY KEY);
2013-02-21 22:34:53 GMT ERROR:  current transaction is aborted, commands 
ignored until end of transaction block
2013-02-21 22:34:53 GMT STATEMENT:  CREATE TABLE foo (id serial PRIMARY KEY);




Further example from python shell:

andyt@andyt-ThinkPad-X61:~$ python
Python 2.7.3 (default, Sep 26 2012, 21:51:14) 
[GCC 4.7.2] on linux2
Type help, copyright, credits or license for more information.
 import psycopg2
 import psycopg2.extras
 db = psycopg2.connect(
... host = 'localhost',
... database = 'postgres',
... user = 'postgres',
... password = 'password'
... )
 cursor_to = db.cursor()
 cursor_to.execute(DROP TABLE IF EXISTS tubecross)
 cursor_to.execute(DROP TABLE tubecross)
Traceback (most recent call last):
  File stdin, line 1, in module
psycopg2.ProgrammingError: table tubecross does not exist

 cursor_to.execute(SELECT * FROM loc_tramlink)
Traceback (most recent call last):
  File stdin, line 1, in module
psycopg2.InternalError: current transaction is aborted, commands ignored until 
end of transaction block


Postgres log:

2013-02-21 23:08:18 GMT LOG:  database system was shut down at 2013-02-21 
23:07:40 GMT
2013-02-21 23:08:18 GMT LOG:  autovacuum launcher started
2013-02-21 23:08:18 GMT LOG:  database system is ready to accept connections
2013-02-21 23:08:18 GMT LOG:  incomplete startup packet
2013-02-21 23:08:19 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 23:08:19 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 23:08:20 GMT FATAL:  password authentication failed for user 
postgres
2013-02-21 23:08:20 GMT FATAL:  password authentication failed for user 

Re: psycopg2 craziness

2013-02-21 Thread andydtaylor
I'd actually forgotten about commit, thanks!

I'll have another go with this in mind.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to do this? List loop (matrix?) iteration

2013-01-09 Thread andydtaylor
Thanks for your help - this is what I did - though it's probably obvious to 
most people reading this.

   for rowcount in range (0, stn_count):
  row_durations.append(stn_list_short[rowcount])
  for colcount in range (0, stn_count): 
 # 3. Determine Station pairs for API query
 query_origin_stop = stn_list_long[rowcount]
 query_destination_stop = stn_list_long[colcount]
 # 4. Paths for determining duration. station x = station x has 
journey time 0
 # 4a. Stations are SAME


etc. and this part works! I am now stuck on something else though, but I'll 
start a new topic for that.

Thanks

Andy
-- 
http://mail.python.org/mailman/listinfo/python-list


Psycopg2 SyntaxError: invalid syntax on INSERT INTO database

2013-01-09 Thread andydtaylor
Hi,

I'm a bit stuck on this INSERT INTO syntax error. I have no idea why it's not 
working actually... I've tried changing column types to char but that didn't 
work. I've gone a bit blind looking at it, but hopefully you can set me right. 
With the '#'d out lines instead the file does work.

What am I missing?

Thanks


Andy


#!/usr/bin/python
import psycopg2
import sys

def main():
   db = psycopg2.connect(
  host = 'localhost',
  database = 'gisdb',
  user = 'postgres',
  password = '##'
   )
   cursor = db.cursor()
   cursor.execute(DROP TABLE IF EXISTS tubecross)
   cursor_to.execute(CREATE TABLE tubecross (id serial PRIMARY KEY, 
station_code char, SAJ interval, SPB interval, SOQ interval);)
   #cursor.execute(CREATE TABLE tubecross (id serial PRIMARY KEY, num integer, 
data varchar);) 
   #cursor.execute(INSERT INTO tubecross (num, data) VALUES (%s, %s),(900, 
9abc'def))
   cursor_to.execute(INSERT INTO tubecross (station_code, SAJ, SPB, SOQ) 
VALUES (%s, %s, %s, %s),(SAJ, 00:00, 00:22, 00:27))
   db.commit()

if __name__ == __main__:
main()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Psycopg2 SyntaxError: invalid syntax on INSERT INTO database

2013-01-09 Thread andydtaylor
Hi John,

He're the code I would like to see work. The cursor_to is an oversight. I 
extracted this element from some other code in an attempt to isolate/resolve 
the problem myself, hence having a simplified table version. Which works 
actually, but unfortunately that's not educating me suffieciently. Actual error 
message I see follows.

- - - - - - - - - - - - - - - - - - - - - - - - - 
Code:

#!/usr/bin/python
import psycopg2
import sys

def main():
   db = psycopg2.connect(
  host = 'localhost',
  database = 'gisdb',
  user = 'postgres',
  password = '##' 
   )
   cursor = db.cursor()
   cursor.execute(DROP TABLE IF EXISTS tubecross)
   cursor.execute(CREATE TABLE tubecross (id serial PRIMARY KEY, station_code 
char, SAJ interval, SPB interval, SOQ interval);)
   cursor.execute(INSERT INTO tubecross (station_code, SAJ, SPB, SOQ) VALUES 
(%s, %s, %s, %s),(SAJ, 00:00, 00:22, 00:27))
   db.commit()

if __name__ == __main__:
main()

- - - - - - - - - - - - - - - - - - - - - - - - - 
Error Message:

andyt@andyt-ThinkPad-X61:~/projects/django-stringer/Other/TFLJPAPI$ python 
creat_db_exp.py
  File creat_db_exp.py, line 15
cursor.execute(INSERT INTO tubecross (station_code, SAJ, SPB, SOQ) VALUES 
(%s, %s, %s, %s),(SAJ, 00:00, 00:22, 00:27))

 ^
SyntaxError: invalid syntax


Thanks for your help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Psycopg2 SyntaxError: invalid syntax on INSERT INTO database

2013-01-09 Thread andydtaylor
Thanks for your help guys. 

I was actually doing a few things wrong, but I have got this script to work by 
declaring fields as varchar and all values as strings. But I would like to log 
journey time values in hours/minutes, so I will have to look into the following:

1. Retrieving this data from postgres as text, converting it and using it. I 
will need to add/subtract on this time value; or
2. Recognising it as a time class in the first instance by using the string 
parsing function.

Regards,

Andy
-- 
http://mail.python.org/mailman/listinfo/python-list


Best way to do this? List loop (matrix?) iteration

2013-01-08 Thread andydtaylor
Hi!

I might be missing the obvious, or I may have found something more complicated 
than the VBA I am used to. Could it be I need to use a maths library?

For a given list of k items I'd like to turn it into an k*k matrix of item 
pairs.

List_sample = ['a', 'b', 'c']

Output:

aa ab ac
ba bb bc
ca cb cc

I'd like to have 2 hooks into this process
1. I want the opportunity to use a value pair each time they are generated 
(because I need to send these to an api and get a number back to put into a 
temporary list or dictionary - still tbd).
2. I'd also like to know each time a row is completed so I can bank that 
temporary list to a database table. Else build one big list and do it at the 
end, I'm still figuring this out.

#Code I've tried:

   stn_count = len(stn_list_short)
   for rowcount in range (0, stn_count):
  for colcount in range (0, stn_count):
 print stn_list_long[rowcount] stn_list_long[colcount]

I've found itertools, tee, and product and felt I was getting warmer. I'm still 
looking, but any pointers would be appreciated!

Thanks,

Andy


-- 
http://mail.python.org/mailman/listinfo/python-list


Difference between these two lists?

2013-01-07 Thread andydtaylor
Hi,

Python newbie here again - this is probably a quick one. What's the difference 
between the lines I've numbered 1. and 2. below, which produce the following 
results: 

Results:
1. [ANG, BAR, BPK, CTN, QGH, QHD, KXX]
2. ['ANG', 'BAR', 'BPK', 'CTN', 'QGH', 'QHD', 'KXX']

Code:
   cursor_from.execute('SELECT * FROM tubestations LIMIT 1000')
   
   stn_list_short = []
   for row in cursor_from:
  if row[4]:
# Station values for database
stn_list_short.append(row[5])

   1. print stn_fields = '[%s]' % ', '.join(map(str, stn_list_short))  
   2. print stn_list_short

Thanks!

Andy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between these two lists?

2013-01-07 Thread andydtaylor
I think I can answer my own question on reflection the first one is 
actually  a string I think? I was confused by the square brackets around the 
placeholder %s.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between these two lists?

2013-01-07 Thread andydtaylor
Thanks, I think I'm clear now. 

I guess (map(str, stn_list)) was all about how to make a string starting with 
integers. I picked that up and began using it without realising it was over 
catering for a list already containing strings, and join(stn_list) was really 
all I required.

Repr and Eval I think I get. Eval certainly. That's a familiar concept, and one 
I hope to use tomorrow to feed a line to psycopg2. 

I might have opened a can of worms on map and repr though... I'll do some more 
reading tomorrow.

Top tip on the code annotation, I didn't really think that through.

Thanks again,

Andy
-- 
http://mail.python.org/mailman/listinfo/python-list


psycopg2 cursor.execute CREATE TABLE issue

2013-01-06 Thread andydtaylor
Hi all,

I'm trying to create a process which will create a new table and populate it. 

But something is preventing this from working, and I don't know enough to 
figure it out, despite having spent most of today reading up. The code executes 
with no error, yet no table is created or populated.

Can anyone offer me some advice? code below. 

Thanks,

Andy

#!/usr/bin/python
import psycopg2
import sys

def main():
   db = psycopg2.connect(
  host = 'localhost',
  database = 'gisdb',
  user = 'postgres',
  password = 'L1ncoln0ut@'
   )
   cursor = db.cursor()
   cursor.execute(CREATE TABLE test (id serial PRIMARY KEY, num integer, data 
varchar);)
   cursor.execute(INSERT INTO test (num, data) VALUES (%s, %s),(100, 
abc'def))

if __name__ == __main__:
main()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: psycopg2 cursor.execute CREATE TABLE issue

2013-01-06 Thread andydtaylor
Wow it's as simple as that! I'm afraid my database experience is in Microsoft 
Access in Windows and not at the command line, so that wasn't intuitive for me.

Thanks again,

Andy
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Evaluate postgres boolean field

2013-01-05 Thread andydtaylor
Brilliant, thanks guys
-- 
http://mail.python.org/mailman/listinfo/python-list


Evaluate postgres boolean field

2013-01-04 Thread andydtaylor
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', 
'0101E0E6108851AB9E9803B9BF5BB6972294C24940',
 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
-- 
http://mail.python.org/mailman/listinfo/python-list