Build SQLite extension on Linux: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
(+crossref)
(cosmetics)
 
Line 14: Line 14:
   wget -c "https://sqlite.org/contrib/download/extension-functions.c/download/extension-functions.c?get=25" -O extension-functions.c
   wget -c "https://sqlite.org/contrib/download/extension-functions.c/download/extension-functions.c?get=25" -O extension-functions.c


* Compile the file into an extra library:
* Compile the file to get the SQLite extension function library:
** You can simply save this convenient Makefile (note: indentation must the done with <tab>!) into the same directory in which extension-functions.c is stored:
** You can simply save this convenient Makefile (note: indentation must the done with <tab>!) into the same directory in which extension-functions.c is stored (for example, $HOME/software/sqlite_extra):


<source lang="Makefile">
<source lang="Makefile">
Line 30: Line 30:
@rm -f libsqlitefunctions.so
@rm -f libsqlitefunctions.so
</source>
</source>
** To compile then just run "make".
** To compile then just run "make" within the directory where you saved extension-functions.c and the Makefile. If all went fine, you now also have the libsqlitefunctions.so in the directory.


Now '''libsqlitefunctions.so''' is built and can be used by v.db.update (example from the manual):
Now '''libsqlitefunctions.so''' is built and can be used by v.db.update:


    # Windows
     g.copy vect=precip_30ynormals,myprecip_30ynormals
     g.copy vect=precip_30ynormals,myprecip_30ynormals
     v.db.addcolumn myprecip_30ynormals column="logjuly double precision"
     v.db.addcolumn myprecip_30ynormals column="logjuly double precision"
     v.db.update myprecip_30ynormals column="logjuly" query_column="log(jul)" \
     v.db.update myprecip_30ynormals column="logjuly" query_column="log(jul)" \
       sqliteextra=C:\OSGeo4Wdev\src\sqliteextension\libsqlitefunctions.dll
       sqliteextra=C:\OSGeo4Wdev\src\sqliteextension\libsqlitefunctions.dll
   
    # Linux
    g.copy vect=precip_30ynormals,myprecip_30ynormals
    v.db.addcolumn myprecip_30ynormals column="logjuly double precision"
    v.db.update myprecip_30ynormals column="logjuly" query_column="log(jul)" \
      sqliteextra=$HOME/software/sqlite_extra/libsqlitefunctions.so


=== See also ===
=== See also ===

Latest revision as of 21:08, 6 November 2018

Problem: SQLite lacks by default log(), sqrt(), and other functions.

Solution: In v.db.update there is the following option:

   sqliteextra=name
   Name of SQLite extension file for extra functions (SQLite backend only)

For build SQLite extension for Linux follow these steps:

  • Install a compiler like "gcc"
  • Install the SQLite development package (named "sqlite-dev" or "sqlite-devel")
  • Download extension-functions.c from here:
 wget -c "https://sqlite.org/contrib/download/extension-functions.c/download/extension-functions.c?get=25" -O extension-functions.c
  • Compile the file to get the SQLite extension function library:
    • You can simply save this convenient Makefile (note: indentation must the done with <tab>!) into the same directory in which extension-functions.c is stored (for example, $HOME/software/sqlite_extra):
# https://sqlite.org/contrib/download/extension-functions.c

default:
	wget -c "https://sqlite.org/contrib/download/extension-functions.c/download/extension-functions.c?get=25" -O extension-functions.c
	gcc -fPIC -lm -shared extension-functions.c -o libsqlitefunctions.so
	@echo "Usage:"
	@echo "  sqlite> SELECT load_extension('/path/to/libsqlitefunctions.so');"
	@echo "  sqlite> select value, log(value) from test;1|0.0"

clean:
	@rm -f libsqlitefunctions.so
    • To compile then just run "make" within the directory where you saved extension-functions.c and the Makefile. If all went fine, you now also have the libsqlitefunctions.so in the directory.

Now libsqlitefunctions.so is built and can be used by v.db.update:

   # Windows
   g.copy vect=precip_30ynormals,myprecip_30ynormals
   v.db.addcolumn myprecip_30ynormals column="logjuly double precision"
   v.db.update myprecip_30ynormals column="logjuly" query_column="log(jul)" \
     sqliteextra=C:\OSGeo4Wdev\src\sqliteextension\libsqlitefunctions.dll
   
   # Linux
   g.copy vect=precip_30ynormals,myprecip_30ynormals
   v.db.addcolumn myprecip_30ynormals column="logjuly double precision"
   v.db.update myprecip_30ynormals column="logjuly" query_column="log(jul)" \
     sqliteextra=$HOME/software/sqlite_extra/libsqlitefunctions.so

See also