The only thing is, I wanted to create a few custom PHP pages and didn't really want to use MySQL, instead I wanted to do some simple things with SQLite. This proved a bit harder than I thought. It started out ok, I simply apt-get'd the php5-sqlite package, restarted apache and created a little sample I found here (listed below):
query('SELECT requests FROM tablename WHERE id = 1'); if ($q === false) { $db->queryExec('CREATE TABLE tablename (id int, requests int, PRIMARY KEY (id)); INSERT INTO tablename VALUES (1,1)'); $hits = 1; } else { $result = $q->fetchSingle(); $hits = $result+1; } $db->queryExec("UPDATE tablename SET requests = '$hits' WHERE id = 1"); } else { die($err); } |
And guess what? It worked! Well I've come to expect that with PHP in the limited time I've been using it, things just seem to setup and work properly and that's tough to admit. Being a Java guy, I'm really whoring around here playing with PHP, but truthfully I just wanted to code a little project where I didn't have to fight tooth and nail figuring out what frameworks to use. Not to mention the ping pong match of "change->build->start servlet engine->repeat", I just want to mess around with an easy chick (don't tell Java though, I love her!)
Anyway, yes the code sample worked but I noticed that I could not for the life of me open the sqlite database the code created using the sqlitebrowser. It kept saying that the database wasn't version 3. Sigh. So I looked through the available packages for Ubuntu and found like every permutation of php
and sqlite
along with version numbers for each. I tried them all, each would bomb with various errors. Finally I stumbled onto this thread which states:
There is no bug, because there is no such thing as PDO::SQlite3
You can use the string sqlite as a DSN for the PDO driver, as in
$dbHandle = new PDO('sqlite:test-sqlite3.db');which opens a connection to a sqlite3 database.
The PDO sqlite dirver supports both sqlite2 and sqlite3 and is installed by the package php-sqlite.
To connect to a sqlite2 database, you would use
$dbHandle = new PDO('sqlite2:test-sqlite2.db');The package php-sqlite3 installs the PECL SQLite3 extension,which is a completely different animal.
It is used as follows:
$dbHandle = new SQLite3('test.db');
So, choosing php5-sqlite
for my package, and changing my code to match the proper api calls did it. My code now works like a charm and the resulting database is version 3+ of SQLite. Note how SQLite3 doesn't have it's own PDO object, you simply use the standard one and feed it a database prefix of sqlite
and it knows to connect to an SQLite3 db. If you wanted to connect to an SQLite2 db, you would use the prefix sqlite2
.
query('SELECT requests FROM tablename WHERE id = 1'); if ($q === false) { $db->exec('CREATE TABLE tablename (id int, requests int, PRIMARY KEY (id)); INSERT INTO tablename VALUES (1,1)'); $hits = 1; } else { $result = $q->fetchColumn(); $hits = $result+1; } $db->exec("UPDATE tablename SET requests = '$hits' WHERE id = 1"); } else { die($err); } echo $result; |