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
88 """!Perform SQL select statement
90 @param table table name
91 @param sql SQL select statement (string or file)
92 @param file True if sql is filename
93 @param args see db.select arguments
112 fatal(_(
"Fetching data from table <%s> failed") % table)
115 result =
map(
lambda x: x.rstrip(os.linesep), ofile.readlines())