Used sqlite3.Connection.load_extension() instead of query execution for SpatiaLite loading.

This commit is contained in:
Sergey Fedoseev 2017-09-12 08:27:29 +05:00 committed by Tim Graham
parent 0dbcd0e87f
commit f36e5d68d9
1 changed files with 3 additions and 6 deletions

View File

@ -3,7 +3,7 @@ from ctypes.util import find_library
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.db.backends.sqlite3.base import ( from django.db.backends.sqlite3.base import (
DatabaseWrapper as SQLiteDatabaseWrapper, SQLiteCursorWrapper, DatabaseWrapper as SQLiteDatabaseWrapper,
) )
from .client import SpatiaLiteClient from .client import SpatiaLiteClient
@ -46,16 +46,13 @@ class DatabaseWrapper(SQLiteDatabaseWrapper):
'SpatiaLite requires SQLite to be configured to allow ' 'SpatiaLite requires SQLite to be configured to allow '
'extension loading.' 'extension loading.'
) )
# Loading the SpatiaLite library extension on the connection, and returning # Load the SpatiaLite library extension on the connection.
# the created cursor.
cur = conn.cursor(factory=SQLiteCursorWrapper)
try: try:
cur.execute("SELECT load_extension(%s)", (self.spatialite_lib,)) conn.load_extension(self.spatialite_lib)
except Exception as exc: except Exception as exc:
raise ImproperlyConfigured( raise ImproperlyConfigured(
'Unable to load the SpatiaLite library extension "%s"' % self.spatialite_lib 'Unable to load the SpatiaLite library extension "%s"' % self.spatialite_lib
) from exc ) from exc
cur.close()
return conn return conn
def prepare_database(self): def prepare_database(self):