1 """!@package grass.script.db
3 @brief GRASS Python scripting module (database functions)
5 Database related functions to be used in Python scripts.
10 from grass.script import db as grass
12 grass.db_describe(table)
16 (C) 2008-2009 by the GRASS Development Team
17 This program is free software under the GNU General Public
18 License (>=v2). Read the file COPYING that comes with GRASS
21 @author Glynn Clements
22 @author Martin Landa <landa.martin gmail.com>
25 import tempfile
as pytempfile
31 gettext.install(
'grasslibs', os.path.join(os.getenv(
"GISBASE"),
'locale'), unicode=
True)
34 """!Return the list of columns for a database table
35 (interface to `db.describe -c'). Example:
38 >>> grass.db_describe('lakes')
39 {'nrows': 15279, 'cols': [['cat', 'INTEGER', '11'], ['AREA', 'DOUBLE PRECISION', '20'],
40 ['PERIMETER', 'DOUBLE PRECISION', '20'], ['FULL_HYDRO', 'DOUBLE PRECISION', '20'],
41 ['FULL_HYDR2', 'DOUBLE PRECISION', '20'], ['FTYPE', 'CHARACTER', '24'],
42 ['FCODE', 'INTEGER', '11'], ['NAME', 'CHARACTER', '99']], 'ncols': 8}
45 @param table table name
48 @return parsed module output
50 s =
read_command(
'db.describe', flags =
'c', table = table, **args)
52 fatal(_(
"Unable to describe table <%s>") % table)
56 for l
in s.splitlines():
59 f[1] = f[1].lstrip(
' ')
60 if key.startswith(
'Column '):
61 n = int(key.split(
' ')[1])
63 elif key
in [
'ncols',
'nrows']:
64 result[key] = int(f[1])
74 """!Return the current database connection parameters
75 (interface to `db.connect -p'). Example:
78 >>> grass.db_connection()
79 {'group': 'x', 'schema': '', 'driver': 'dbf', 'database': '$GISDBASE/$LOCATION_NAME/$MAPSET/dbf/'}
82 @return parsed output of db.connect
87 def db_select(sql = None, filename = None, table = None, **args):
88 """!Perform SQL select statement
90 Note: one of <em>sql</em>, <em>filename</em>, or <em>table</em>
91 arguments must be provided.
96 grass.db_select(sql = 'SELECT cat,CAMPUS FROM busstopsall WHERE cat < 4')
98 (('1', 'Vet School'), ('2', 'West'), ('3', 'North'))
102 grass.db_select(filename = '/path/to/sql/file')
108 grass.db_select(table = 'busstopsall')
111 performs <tt>SELECT * FROM busstopsall</tt>.
113 @param sql SQL statement to perform (or None)
114 @param filename name of file with SQL statements (or None)
115 @param table name of table to query (or None)
116 @param args see db.select arguments
122 args[
'input'] = filename
124 args[
'table'] = table
126 fatal(_(
"Programmer error: '%(sql)s', '%(filename)s', or '%(table)s' must be provided") %
127 {
'sql':
'sql',
'filename':
'filename',
'table':
'table'} )
138 fatal(_(
"Fetching data failed"))
141 result = map(
lambda x: tuple(x.rstrip(os.linesep).
split(args[
'fs'])),
def db_connection
Return the current database connection parameters (interface to `db.connect -p'). ...
def db_describe
Return the list of columns for a database table (interface to `db.describe -c').
def split
Platform spefic shlex.split.
def fatal
Display an error message using g.message -e, then abort.
def parse_key_val
Parse a string into a dictionary, where entries are separated by newlines and the key and value are s...
def try_remove
Attempt to remove a file; no exception is generated if the attempt fails.
def db_select
Perform SQL select statement.
def run_command
Passes all arguments to start_command(), then waits for the process to complete, returning its exit c...
def read_command
Passes all arguments to pipe_command, then waits for the process to complete, returning its stdout (i...