[GRASS-user] POW() in SQLite?

Hi,

I am trying to use POW(x,n) with SQLite but it fails:

G65> v.db.select mapname column="POW(length,3)"
DBMI-SQLite driver error:
Error in sqlite3_prepare():SELECT POW(length,3) FROM mapname
no such function: POW
ERROR: Unable to open select cursor

Similar problem if I try x^n:
...
unrecognized token: "^"
ERROR: Unable to open select cursor

What I really want to do is add that value as a new column:
G65> v.db.update mapname column=volume value="POW(length,3)" --verbose

but I get the same error. It doesn't work with DBF either, but I sort
of expected that.

this random webhit suggests that it should be possible:
  http://osdir.com/ml/sqlite-users/2009-03/msg00248.html

any ideas?

not a DB guru,
Hamish

On Mon, 1 Mar 2010, Hamish wrote:

I am trying to use POW(x,n) with SQLite but it fails:

Hamish,

   Mathematical functions such as POW() are not part of SQL. When you look at
what appear to be mathematical functions, e.g., ABS(). MIN(), MAX(), they
are actually string manipulations.

   SQLite stores most values as text strings so this is not surprising.

   I'd manipulate those values first, then pass them to SQLite.

Rich

Hamish wrote:

I am trying to use POW(x,n) with SQLite but it fails:

G65> v.db.select mapname column="POW(length,3)"
DBMI-SQLite driver error:
Error in sqlite3_prepare():SELECT POW(length,3) FROM mapname
no such function: POW

Nope; not in SQLite:

  http://www.sqlite.org/lang_corefunc.html

ERROR: Unable to open select cursor

Similar problem if I try x^n:
...
unrecognized token: "^"

SQLite doesn't have a power operator:

  http://www.sqlite.org/lang_expr.html#binaryops

this random webhit suggests that it should be possible:
  http://osdir.com/ml/sqlite-users/2009-03/msg00248.html

That says:

* Mathematical SQL Functions - The following mathematical SQL functions are
available in addition to the SQLite default: ACOS(), ASIN(), ATAN(), ATAN(),
ATAN2(), CEIL(), CEILING(), COS(), COT(), DEGREES(), EXP(), FLOOR(), LN(),
LOG(), LOG(), LOG2(), LOG10(), MOD(), PI(), POW(), RADIANS(), SIGN(), SIN(),
SQRT(), TAN(), TRUNCATE().

Note: "in addition to the SQLite default". IOW, those functions are
added by SQLiteSpy, and are not part of SQLite.

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

Hamish wrote:
> I am trying to use POW(x,n) with SQLite but it fails:

...

> no such function: POW

Glynn:

Nope; not in SQLite:
    http://www.sqlite.org/lang_corefunc.html

..

> Similar problem if I try x^n:
> unrecognized token: "^"

SQLite doesn't have a power operator:
    http://www.sqlite.org/lang_expr.html#binaryops

> this random webhit suggests that it should be possible:
> http://osdir.com/ml/sqlite-users/2009-03/msg00248.html

That says:

..

Note: "in addition to the SQLite default". IOW, those
functions are added by SQLiteSpy, and are not part of SQLite.

ah, skimming error.

Rich:

I'd manipulate those values first, then pass them to SQLite.

solution:

#no good:
#v.db.update basemap column=volume value="POW(length,3)" --verbose

#good:
v.db.select basemap column=cat,length | awk -F'|' \
  '{printf("UPDATE basemap SET volume=%f WHERE cat=%d;\n", $2^3, $1)}' \
  | db.execute

thanks,
Hamish