Archive for the ‘Projects’ Category

New Project: SPDO

January 28th, 2009 by Sjan Evardsson

I have just posted the (embarrassingly empty) page for my new pet project: SPDO (Sjan’s PDO) – in two flavors: PHP and Python.

There are only about a thousand PDOs out there, and perhaps a dozen or so of them are really functional and (in a few cases) very polished pieces of work. So why am I messing with writing my own? A couple of reasons:

  1. I like to have coding signatures that are consistent, whether I am working with PostgreSQL, MySQL or (heaven help us all) SQLite.
  2. I like to have coding signatures that are (reasonably) consistent across different languages – in this case Python and PHP.
  3. I wanted to take advantage of Prepared Statements where I could, even though the PHP implementations of those are pretty weak (especially in the case of MySQL).

Currently implemented in

  • Python:
    • PostgreSQL (with prepared statements)
    • SQLite (no prepared statements).
  • PHP
    • PostgreSQL (with prepared statements)
    • MySQL (with prepared statements)
    • SQLite (no prepared statements)

Here’s an example of how they work (in most simplistic terms):

Python [Show Plain Code]:
  1. #in Python
  2. from pyDB import *
  3. db = pyDB(‘mysite’)
  4. newid = db.insert(‘INSERT INTO test (name, value) VALUES (?,?)’,[‘foo’,‘bar’])
  5. update_count = db.update(‘UPDATE test SET value=? WHERE id=?’,[‘baz’,newid])
  6. results = db.select(‘SELECT * FROM test’)
  7. for row in results:
  8.     for i in row:
  9.         print "\t", i,"\t", row[i]
  10. delete_count = db.delete(‘DELETE FROM test WHERE id=?’,[newid])
  1. //in PHP
  2. require_once(‘phpdb.php’);
  3. $db = new phpDB(‘test’);
  4. $newid = $db->insert(‘INSERT INTO test (name, value) VALUES (?,?)’,array(‘foo’,‘bar’));
  5. $update_count = $db->update(‘UPDATE test SET value=? WHERE id=?’,array(‘baz’,newid));
  6. $results = db->select(‘SELECT * FROM test’);
  7. foreach($results as $row)
  8. {
  9.     foreach ($row as $key=>$val)
  10.     {
  11.         print "\t$key\t$val";
  12.     }
  13. }
  14. $delete_count = $db->delete(‘DELETE FROM test WHERE id=?’,array($newid));

The page with links to the code is in the list up top, and everything is MIT license. Enjoy.

Post to Twitter Post to Yahoo Buzz Buzz This Post Post to Delicious Delicious Post to Digg Digg This Post Post to Ping.fm Ping This Post Post to Reddit Reddit Post to StumbleUpon Stumble This Post