Reputation: 14782
I've got an PHP installation without the SQLite-Functionality as a base install so no sqlite_* functions are available.
Is there a PHP library (PHP code) that can access SQLite Databases without the need of installing any plugins into PHP?
(I'm not able to change the server configuration)
In fact i only need basic support (SELECT Statements only)
Basically i'm looking for a pure-PHP SQLite driver much like https://github.com/kripken/sql.js is a pure-JS implementation of the SQLite driver.
Upvotes: 6
Views: 2874
Reputation: 631
maybe I'm a bit late with my answer, but I'm sure it can be helpful to other users looking for this kind of questions. SQLite is an exceptional piece of software, and sometimes it can replace MySQL or other classically used databases. (let's think about low-traffic websites..) Unluckily, it's available as a PHP extension, and so can be disabled server side when relying on third party cheap hosting providers. This is why I started developing PHPFileDB, a complete flat file database coded in PHP with full support to SQL syntax (my target is to obtain a complete compatibility with MySQL based frameworks)
Obviously, it is open source and accessible at https://github.com/morepaolo/PHPFileDB
Hoping to be helpful!
Upvotes: 1
Reputation: 4868
Different question is if it is clever enough to actually do that...
I think that better way to is to import sqlite databases to mysql or to some other db that is usable.
Okay, this still requires that sqlite
is installed...
Here's how it can be done:
class ExtremelySimpleAndPowerfulSQLite {
private $database_file;
public function __construct( $filename ) {
$this->database_file = $filename;
}
public function sqlite_query( $sql ) {
ob_start();
passthru("sqlite ".$this->database_file." '$sql'", $result);
if ($result <> 0) {
ob_end_clean();
return false;
}
$sqlite_result = ob_get_contents();
ob_end_clean();
return $sqlite_result;
}
}
$sqlite = new ExtremelySimpleAndPowerfulSQLite("test.db");
// It works, huh...
echo $sqlite->sqlite_query('select * from table');
if ($foobar = $sqlite->sqlite_query('select * from mytable'))
echo $foobar;
this does sqlite without those php's own sqlite functions, so...
Upvotes: 2
Reputation: 10339
I didn't found any plain php sqlite driver searching on google. I tried to get something out of pear but the sqlite mdb2 driver make use of the sqlite php extension to work:
root@blackbigone:~# pear install MDB2_Driver_sqlite
pear/MDB2_Driver_sqlite requires PHP extension "sqlite"
No valid packages found
install failed
After I installed php-sqlite the pear module gone ok:
root@blackbigone:~# pear install MDB2_Driver_sqlite
downloading MDB2_Driver_sqlite-1.4.1.tgz ...
Starting to download MDB2_Driver_sqlite-1.4.1.tgz (30,921 bytes)
.........done: 30,921 bytes
install ok: channel://pear.php.net/MDB2_Driver_sqlite-1.4.1
Reading the code, any of the php sqlite wrappers I found have something like this:
if (!function_exists('sqlite_open')) return false;
So, I think nobody written a pure sqlite driver in PHP yet. Sorry.
Upvotes: 0
Reputation: 14304
Any php library is either written in pure php and uses some underlying functions, or is a module, which should be installed.
Hence the only option is taking C code for that module and rewriting it into php. AFAIK, the only low-level functions required, are working with network sockets and they are available in php.
Upvotes: 1