6

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.

5
  • How old is this install? Sounds like you could do with updating your PHP install to me, SQLite has been part of PHP core for yonks... Commented Apr 18, 2012 at 12:00
  • 1
    Do you have access to PDO? You can use phpinfo() to check. Search the output of that function for the strings sqlite or sqlite2. Commented Apr 18, 2012 at 12:06
  • It's PHP5.3 but without the SQLite module compiled into it. I can't update it. It's basically not available since the filesystem is read-only and SQLite-Databases wouldn't make much sense, but since i'm only reading... - And yes i got access to PDO but not sqlite or sqlite2 inside PDO. Commented Apr 18, 2012 at 12:26
  • there are 100500 ways to handle data without SQL. Go figure. You may have all your selects in a matter of couple hours. But you prefer to spend a week looking for a miracle Commented Apr 21, 2012 at 3:47
  • @YourCommonSense This is more out of curiosity. If it's not possible/available i'll use a db server. I'm not bound to using SQLite, but i got the data as SQLite and not needing to convert/import/export it in any way would be great. Commented Apr 21, 2012 at 10:48

4 Answers 4

2
+50

Everything is possible

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;

But

this does sqlite without those php's own sqlite functions, so...

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for pure and freakisly mindless creativity :D | well while this fit's the question perfectly i surely hope one could infer from the question that command line commands are pretty much non-available too.
This isn't exactly what i wanted but it's by far the best answer and made me lol so you deserve the bounty.
Props for "Everything is possible" and the ingenuity of it all. Made me smile.
It is actually using sqlite but called by the command line, you should write this as a preamble
1

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.

2 Comments

Yes, and i'm looking for a pure-PHP implementation of the SQLite driver :)
With SQLite, the library is the entire thing. A feature-compatible PHP implementation of the library would be a complete rewrite of the entire project!
1

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!

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.