Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load sqlite extension in PDO?

Tags:

php

sqlite

pdo

First, I want to inform about the case to avoid the misunderstanding.

By sqlite extension, I mention Sqlite's extension like FTS, not PHP's sqlite extension.

I have been using PDO Sqlite in my application, it cannot be changed.

As I saw here, Sqlite extensions can be loaded as query seen below:

SELECT load_extension('xyz.so');
$db = new PDO ( 'sqlite:qwert.db' );
$db->query("SELECT load_extension('myextension.so');");
$db->query("SELECT myfunction(name) FROM table");
$rows = $db->fetchAll(PDO::FETCH_CLASS, 'stdClass');

Note: myfunction is method of myextension

But when I test with this query from PDO, it return "not authorized" message.

For only testing purpose, I tried PHP's Sqlite3 extension to load the extension by using the code below:

$db = new SQLite3('qwer.db');
$db->loadExtension('xyz.so');

It works

As I know that PDO Sqlite hasnot a method like loadExtension for loading extensions

Any idea how can I handle this?

like image 783
Umut KIRGÖZ Avatar asked Jan 06 '12 10:01

Umut KIRGÖZ


People also ask

Does PDO work with SQLite?

PDO (PHP Data Objects) abstracts database access and enables you to create code that can handle different types of databases. One of the database types that PDO supports is SQLite.

What is PDO SQLite?

PDO_SQLITE is a driver that implements the PHP Data Objects (PDO) interface to enable access to SQLite 3 databases. Note: PDO_SQLITE allows using strings apart from streams together with PDO::PARAM_LOB .


1 Answers

I have implemented a library with a solution for this problem for PHP 7.4+ which does not involve recompiling the PDO driver. In short, it uses PHP's FFI extension and Z-Engine (which reads PHP's internal memory structures using FFI) to make a direct call to the SQLite C API. You can find more detailed background information in this blog post. I would not recommend this solution in production yet, but it could be a feasible workaround for test or development workflows.

like image 186
Arnout Boks Avatar answered Nov 07 '22 11:11

Arnout Boks