本文介绍了“适配器不支持几何形状".声明几何字段时出现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,尝试创建类型为"geometry()"的字段时,引发了适配器不支持几何图形"异常.对于我的测试应用程序,我使用的是sqlite数据库(生产将使用postgres):

In my application, an "Adapter does not support geometry" exception is being thrown when attempting to create a field of type, "geometry()". For my test application, I'm using an sqlite DB (production will use postgres):

db = DAL('sqlite://storage.sqlite', pool_size = 1, fake_migrate_all= False)

有问题的数据库表在模块内部的类中声明,并且包含几个字段,其中一些包含位置数据:

The DB table in question is declared within a class, inside of a module, and contains a several fields, some of which contain location data:

from gluon.dal import Field, geoPoint, geoLine, geoPolygon

class Info(Base_Model):

    def __init__(...):

        try:
            db.define_table('t_info',
                ...
                Field('f_geolocation', type='geometry()',
                    label =  current.T('Geolocation')),
                Field('f_city', type='string',
                    label = current.T('City')),
                ...
        except Exception as e:
            ...

根据Anthony的建议,我已将DAL构造函数调用修改为以下内容:

As per Anthony's suggestion, I've modified the DAL constructor call to the following:

db = DAL('spatialite://storage.sqlite', pool_size = 1)

它会产生以下错误消息:

It produces the following error message:

Traceback (most recent call last):
  File "C:\...\web2py\gluon\restricted.py", line 227, in restricted
    exec ccode in environment
  File "C:/My_Stuff/Programs/web2py/applications/Proj/models/db.py", line 38, in <module>
    db = DAL('spatialite://storage.sqlite', pool_size = 1)
  File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 171, in __call__
    obj = super(MetaDAL, cls).__call__(*args, **kwargs)
  File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 457, in __init__
    raise RuntimeError("Failure to connect, tried %d times:\n%s" % (attempts, tb))
RuntimeError: Failure to connect, tried 5 times:
Traceback (most recent call last):
  File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 435, in __init__
    self._adapter = ADAPTERS[self._dbname](**kwargs)
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 53, in __call__
    obj = super(AdapterMeta, cls).__call__(*args, **kwargs)
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\sqlite.py", line 169, in __init__
    if do_connect: self.reconnect()
  File "C:\...\web2py\gluon\packages\dal\pydal\connection.py", line 129, in reconnect
    self.after_connection_hook()
  File "C:\...\web2py\gluon\packages\dal\pydal\connection.py", line 81, in after_connection_hook
    self.after_connection()
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\sqlite.py", line 177, in after_connection
    self.execute(r'SELECT load_extension("%s");' % libspatialite)
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 1326, in execute
    return self.log_execute(*a, **b)
  File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 1320, in log_execute
    ret = self.cursor.execute(command, *a[1:], **b)
OperationalError: The specified module could not be found.

推荐答案

如果要在SQLite中使用几何字段,则必须使用spatialite适配器,该适配器利用了SQLite的SpatialLite扩展名:

If you want to use geometry fields with SQLite, you must use the spatialite adapter, which makes use of the SpatialLite extension for SQLite:

db = DAL('spatialite://storage.sqlite', pool_size = 1)

请注意,您必须安装了spacespaceite才能起作用.

Note, you must have spatialite installed for this to work.

这篇关于“适配器不支持几何形状".声明几何字段时出现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 16:41