Issue: Spatialite Extension "Function not found"
Author: disarticulate
State: closed
Created: 2016-02-25 01:19:38

following this issue resolution:
https://github.com/coleifer/peewee/issues/292

I'm just plain confused, if the version pops up correctly, this should mean the extension loaded:

I'm getting:

> OperationalError: no such function: ST_GeomFromText
> ([(http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.3.0.html)](http://www.gaia-gis.it/gaia-sins/spatialite-sql-4.3.0.html))

```
from peewee import *
from playhouse.sqlite_ext import SqliteExtDatabase
```

```
def createDatabase(path):
    database = SqliteExtDatabase(path)
    database.connect()
    database.load_extension('mod_spatialite')

    database.create_tables([People, Entities, Affiliations, Projects, Locations, Pictures, Documents, Comments, Notes, LogBook], safe=True)

    sql = 'SELECT sqlite_version(), spatialite_version(),'
    print(database.execute_sql(sql).fetchall())
    sql = 'SELECT InitSpatialMetadata()'
    database.execute_sql(sql)

    database.register_fields({'geometry':'geometry'})
    return database
```

print for version returns:

> [('3.8.11', '4.3.0a')]

Which is Spatialite 4.3.0a

function to update:

```
def geometryFromWKT(wellknowntext):
    return fn.ST_GeomFromText(wellknowntext)
```

Object:

```
class Locations(BaseModel):
    key = PrimaryKeyField()
    daterecord = DateField(null=False, help_text="Date Record", default=datetime.now().date())
    timerecord = TimeField(null=True, help_text="Time Record")
    comments = TextField(null = True, help_text="Comment on the Geometry/Locations details")
    geometry = GeometryField()
    geometrytype = TextField(null = True, help_text="Geometrytype from WKT details")
    geometrytable = TextField(null = True, help_text="Name of Database Table from Geometry Import")
    isdesignfile = BooleanField(null = False, help_text='Is the location part of the project design or not (asbuilt, observation)')
```

field:

```
class GeometryField(Field):
    db_field = 'geometry'
```

Insert method.:

`Locations.insert(geometry=geometryFromWKT('POINT(4` 9 3 2)')).execute()


You need to load the extension every time you connect... Could that be the issue?


```
database.connect()
database.load_extension('mod_spatialite')
sql = 'SELECT sqlite_version(), spatialite_version()'
print(database.execute_sql(sql).fetchall())
Locations.insert(geometry=geometryFromWKT('POINT(4 9 3 `2)')).execute()
```

```
>>[('3.8.11', '4.3.0a')]
> >---------------------------------------------------------------------------
> OperationalError                          Traceback (most recent call last)
> C:\Users\Tsai\Anaconda3\envs\flask\lib\site-packages\peewee.py in execute_sql(self, sql, params, require_commit)
>    3453             try:
> -> 3454                 cursor.execute(sql, params or ())
>    3455             except Exception:
> 
> OperationalError: no such function: ST_GeomFromText
> 
> During handling of the above exception, another exception occurred:
> 
> OperationalError                          Traceback (most recent call last)
> <ipython-input-13-03efd5976860> in <module>()
>       1 database.connect()
>       2 database.load_extension('mod_spatialite')
> ----> 3 Locations.insert(geometry=geometryFromWKT('POINT(4 9 3 2)')).execute()
> 
> C:\Users\Tsai\Anaconda3\envs\flask\lib\site-packages\peewee.py in execute(self)
>    3211             return self._qr
>    3212         else:
> -> 3213             cursor = self._execute()
>    3214             if not self._is_multi_row_insert:
>    3215                 if self.database.insert_returning:
> 
> C:\Users\Tsai\Anaconda3\envs\flask\lib\site-packages\peewee.py in _execute(self)
>    2626     def _execute(self):
>    2627         sql, params = self.sql()
> -> 2628         return self.database.execute_sql(sql, params, self.require_commit)
>    2629 
>    2630     def execute(self):
> 
> C:\Users\Tsai\Anaconda3\envs\flask\lib\site-packages\peewee.py in execute_sql(self, sql, params, require_commit)
>    3459             else:
>    3460                 if require_commit and self.get_autocommit():
> -> 3461                     self.commit()
>    3462         return cursor
>    3463 
> 
> C:\Users\Tsai\Anaconda3\envs\flask\lib\site-packages\peewee.py in __exit__(self, exc_type, exc_value, traceback)
>    3283             else:
>    3284                 exc_args = exc_value.args
> -> 3285             reraise(new_type, new_type(*exc_args), traceback)
>    3286 
>    3287 class _BaseConnectionLocal(object):
> 
> C:\Users\Tsai\Anaconda3\envs\flask\lib\site-packages\peewee.py in reraise(tp, value, tb)
>     125     def reraise(tp, value, tb=None):
>     126         if value.__traceback__ is not tb:
> --> 127             raise value.with_traceback(tb)
>     128         raise value
>     129 elif PY2:
> 
> C:\Users\Tsai\Anaconda3\envs\flask\lib\site-packages\peewee.py in execute_sql(self, sql, params, require_commit)
>    3452             cursor = self.get_cursor()
>    3453             try:
> -> 3454                 cursor.execute(sql, params or ())
>    3455             except Exception:
>    3456                 if self.get_autocommit() and self.autorollback:
> 
> OperationalError: no such function: ST_GeomFromText
```


I'm not at all sure what's going on there. Possibly your SQLite driver is not built with support for Spatialite, or is linked against a different SQLite library? At any rate, I don't believe this is a problem with peewee, which I use with extension modules regularly.


Thanks for the help.

On Fri, Feb 26, 2016 at 12:07 PM, Charles Leifer notifications@github.com
wrote:

> Closed #868 https://github.com/coleifer/peewee/issues/868.
> 
> —
> Reply to this email directly or view it on GitHub
> https://github.com/coleifer/peewee/issues/868#event-567934835.

## 

dis·ar·tic·u·late   Audio pronunciation of "disarticulate" ( P )
Pronunciation Key  (dsär-tky-lt)
v. dis·ar·tic·u·lat·ed, dis·ar·tic·u·lat·ing, dis·ar·tic·u·lates
v. tr.

```
To separate at the joints; disjoint.
```

v. intr.

```
To become disjointed.
```