Nav links

Sunday, 16 September 2007

Dynamic publishing with SQLite 3 in MovableType

MovableType 4 has just been released, but it still hasn't been updated to let you publish dynamically with an SQLite 3 database (SQLite 2 is supported). This is an interesting omission, because dynamic publishing is the best option for low-volume sites, and SQLite 3 is an excellent choice for low volume sites.

PHP 5.1 with SQLite 3 support has been out since 2005, so the developer community, including me, has had plenty of time to get used to the change. Which brings me to the solution.


  1. Reactivate the administration option to publish dynamically



    In file /lib/MT/App/CMS.pm, comment out lines 13277-13280, like so (a '#' at the start of the line tells Perl to ignore the rest of that line):
    #if ( $app->config->ObjectDriver =~ qr/(db[id]::)?sqlite/i ) {
    # $param{hide_build_option} = 1
    # unless $app->config->UseSQLite2;
    #}



  2. Change the PHP calls from the old SQLite functions to the new PHP Data Objects (PDO) functions




    • File /php/lib/mtdb_sqlite.php


      Line 158:

      //$this->result = @sqlite_query($query,$this->dbh);

      $this->result = @$this->dbh->query($query);


      Line 188:
      //if ( $row = sqlite_fetch_array($this->result, SQLITE_ASSOC) )

      if ($row = $this->result->fetch(PDO::FETCH_ASSOC) )



    • File /php/extlib/ezsql/ezsql_sqlite.php

      Line 50:
      //$this->dbh = @sqlite_open($dbpath.$dbname);
      //if ( ! $this->dbh )
      //{
      // $this->print_error("Error","<ol><b>Error establishing a database!</b><li>Are you sure you have the correct path?<li>Are you sure that you have typed the correct database instance name?<li>Are you sure that the database is installed?</ol>");
      //}

      try {
      $this->dbh = new PDO("sqlite:$dbpath$dbname");
      } catch (PDOException $e) {
      $this->print_error($e->getMessage());
      }


      Line 168:

      //$handle = @sqlite_query($query,$this->dbh);
      $handle = $this->dbh->query($query);



      Line 239:

      //while ($row = sqlite_fetch_array($handle, SQLITE_ASSOC)) {
      while ($row = $handle->fetch(PDO::FETCH_ASSOC) ) {


Although there are many further SQLite 2 specific lines of code in MovableType, the above changes were all that were required to get my dynamic blog to work with my SQLite 3 database.