databases icon indicating copy to clipboard operation
databases copied to clipboard

enable load extension for sqlite backend

Open larsclaussen opened this issue 5 years ago • 0 comments

Will databases support loading sqlite extensions in the future? I'm particularly interested in geo extensions like spatialite.

aiosqlite does expose the needed functions as far as I can see:

db = await aiosqlite.connect(db_file)
await db.enable_load_extension(True)
await db.load_extension('mod_spatialite')

After poking around a bit I was able to access the connection object through the Database object

from databases import Database
db = Database(f'sqlite:///{db_file}')
await db.connect()
conn = db.connection().raw_connection
# this fails with AssertionError: Connection is not acquired 

# I have to access the private `SQLiteConnection` instance
await con._connection.acquire()
# now I'm able to get to the raw connection
raw = db.connection().raw_connection
await raw.enable_load_extension(True)
await raw.load_extension('mod_spatialite')
# running a query using the db instance fails, though
query = "SELECT ST_AsText(the_geom) FROM v2_channel" 
await db.execute(query=query)
AssertionError: Connection is already acquired

larsclaussen avatar Nov 22 '19 15:11 larsclaussen