"Unable to execute statement" "no such function : load_extension"

1,123 views
Skip to first unread message

MoZo

unread,
Jan 20, 2011, 4:38:02 AM1/20/11
to SpatiaLite Users
Hi all and sorry for my poor english.

I'm trying to use spatialite 2.4.0 RC with Qt and i have some trouble
when trying to load libspatialite-2.dll :

query.exec("SELECT load_extension('libspatialite-2.dll')");

I've got the error : "Unable to execute statement" "no such
function : load_extension"

here my code Main.cpp :

#include <QtSql>
#include <QtGui>

#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/qmessagebox.h>

#include <QtCore\qdatetime.h>

#include <QtSql\qsqlquery.h>
#include <QtSql\qsqldriver.h>
#include <QtSql\qsqldriverplugin.h>
#include <Qtsql/qsqldatabase.h>
#include <Qtsql/QSqlError>

#include "spatialite/sqlite3.h"
#include "spatialite/gaiaaux.h"
#include "spatialite/gaiageo.h"
#include "spatialite/gaiaexif.h"
#include "spatialite/spatialite.h"
#include "spatialite/sqlite3ext.h"
#include "spatialite.h"

//Version 2.4 RC
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

qDebug() << qVersion();

spatialite_init(0);

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

db.setDatabaseName("test-2.3.sqlite");

QStringList listeDB = QSqlDatabase::drivers () ;
qDebug() << "Drivers :" << listeDB ;


if (!db.open())
{
QMessageBox::critical(0, QObject::tr("Database Error"),
db.lastError().text());
//qDebug() << QSqlError::text();
}
else
{
QVariant v = db.driver()->handle();

if (v.isValid() && qstrcmp(v.typeName(), "sqlite3*")==0)
{
sqlite3 *handle = *static_cast<sqlite3 **>(v.data());

if (handle != 0) { // check that it is not NULL
sqlite3_enable_load_extension(handle,1);
}
}

QSqlQuery query;

bool success = query.exec("SELECT
load_extension('libspatialite-2.dll')");

if (!success) {
QMessageBox::information(
0,
QObject::tr("Query failed"),
query.lastQuery()
);
qDebug() << query.lastError();
}

query.finish();
return app.exec();
}

My Qt version is 4.7.1 and i've the two sqlite dll "qsqlite4.dll" and
"qsqlited4.dll" in a folder named "sqldrivers" near executable.

I've got all the depedency dll near my executable.

Depedency Walker say I'm ok.

Here my .pro :

TEMPLATE = app
TARGET =
DEPENDPATH += .
CONFIG += console


# Input
HEADERS += spatialite.h
HEADERS += spatialite/sqlite3.h
HEADERS += spatialite/gaiaaux.h
HEADERS += spatialite/gaiageo.h
HEADERS += spatialite/gaiaexif.h
HEADERS += spatialite/spatialite.h
HEADERS += spatialite/sqlite3ext.h

LIBS += spatialite_i.lib
LIBS += spatialite.lib

SOURCES += Main.cpp
QT += sql

Don't now why it don't recognize the load_extension...

However, without spatialite extension my sqlite work fine i can make
select/insert/update with Qt

Cordialy MoZo

a.furieri

unread,
Jan 20, 2011, 4:54:33 AM1/20/11
to SpatiaLite Users
i MoZo,

I'm really sorry for you ...
"no such function: load_extension"

translated in plain basic english
this actually means:
"we at QT have decided for you that
using our SQL-related classes you
must not be allowed loading any extension,
because we strongly believe that such
feature is harmful and dangerous"

for any further reference:
http://www.gaia-gis.it/spatialite/spatialite-arch-2.3.1.html#trap

suggestion:
completely skip any QT (or Linux)
pre-packaged sqlite/spatialite.
build them by yourself instead,
directly starting from sources.

bye sandro

MoZo

unread,
Jan 20, 2011, 5:11:46 AM1/20/11
to SpatiaLite Users
Thx Sandro,

Si, actually, I've to rebuild the sqlite dll ( "qsqlite4.dll" and
"qsqlited4.dll") for they accept load_extension right ?

the link you shared talk about "DSQLITE_ENABLE_LOAD_EXTENSION=1", ok
but how ?

sry if it's stupid question...

Thx a lot, Cordialy MoZo

a.furieri

unread,
Jan 20, 2011, 5:40:30 AM1/20/11
to SpatiaLite Users
Hi MoZo,

you surely noticed lots of times
something like this in C/C++ sources:

#ifdef SOMETHING
...
#endif

the above defines a "conditional macro":
the corresponding code section may (or
may not) be actually compiled depending
on the actual setting found at
compilation time.
and this strongly supports portability,
because you can insert two different
implementations (let say, one for WinOZ
and one for Linux) into the same source
simply selecting the right one at compile
time.

so the problem becomes:
how can I define/set SOMETHING ?
there are several ways ...

A) [strongly discouraged]
-----------------------------
open a text editor, and set directly
the option into the source code (at
the very first lines):
#define SOMETHING 1


B) [much more better]
-----------------------------
pass the option as a the compiler arg:
gcc ... -DSOMETHING=1 ...

[this works for MSVC as well:
but in this case /DSOMETHING=1
is expected]

----
And you can use the ENVIRONMENT:
e.g. on Linux
export "CFLAGS=-DSOMETHING=1"

... you can use a Makefile and so on ...
but this strongly depends on your
specific compiler/platform:
please, check the appropriate doc :-)

bye Sandro

MoZo

unread,
Jan 20, 2011, 9:07:43 AM1/20/11
to SpatiaLite Users
Ok, thx a lot Sandro, I open MakeFile.Debug and Makefile.Release in Qt
\4.7.1\src\plugins\sqldrivers\sqlite\ and replace
DSQLITE_OMIT_LOAD_EXTENSION by DSQLITE_ENABLE_LOAD_EXTENSION in both
file and now it work.


Thx again Sincerely MoZo

John Barton

unread,
Feb 15, 2011, 12:11:12 PM2/15/11
to SpatiaLite Users
Hey guys,

Just got SpatiaLite working with my Qt application in Windows /
VS2008.
To get SQLite to build properly with Qt, these changes must be made in
the 4.7.1\src\3rdparty\sqlite.pri file.
Remove the OMIT_EXTENSION flag, it should look like this:

DEFINES += SQLITE_ENABLE_LOAD_EXTENSION SQLITE_ENABLE_RTREE
SQLITE_OMIT_COMPLETE

I'm pretty sure I also used the latest SQLite release instead of the
default Qt build.
After making the change, you need to recompile Qt. From the VS2008
Command Prompt(Tools):
Qt\4.7.1\configure
Qt\4.7.1\nmake

This may take a while. After compilation, add the SpatiaLite .dll's
to your application directory, and you should be
able to load them with a SELECT query.

Hope this helps anyone looking for this info.

John
Reply all
Reply to author
Forward
0 new messages