[GRASS-user] Accessing GRASS databases from Python

Glynn Clements glynn at gclements.plus.com
Thu Jan 20 20:58:35 EST 2011


Joshua Arnott wrote:

> I've had a look at the database functions in 
> the grass.script.db python library, but haven't had any luck getting the 
> db_select function to work. In the default install, the function didn't 
> even exist in db.py. I've added it, based on the current source, but all 
> I get out is:
> 
>  >>> grass.script.db.db_select('Population_Density','select * from 
> Population_Density')
> Sorry, <output> is not a valid parameter
> []

That parameter was added in 7.0. The 6.x version of db.select always
writes to stdout.

You can just run db.select using grass.script.pipe_command(), e.g.:

	import grass.script as grass

	ps = grass.pipe_command(
	    'db.select',
	    quiet = True,
	    flags = 'c',
	    table = table,
	    sql = sql)

	for line in ps.stdout:
	    fields = line.strip().split('|')
	    ...

	ps.wait()

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-user mailing list