LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Thursday, December 20. 2007

Sqlite Version Mismatch

I ran into an issue recently in testing a site where PDO_SQLITE was claiming that it could not read my PDO database files. The only recent change I'd had was that I'd installed a new version of PHP, and hence a new version of PDO_SQLITE. Searching the web (we're not supposed to say googling or googled anymore, remember ;-)), I found that the issue was that the version of sqlite compiled into my PHP install was not compatible with the version I used to create the databases in the first place. Never mind that they're only a micro version or two different.

So, I was left with a conundrum: I needed to create files compatible with my PDO_SQLITE install, but my CLI sqlite tool was incompatible. And if I used PDO_SQLITE to create the db file, I'd lose my data, right?

Wrong. And here's what you can do should you find yourself in the same situation sometime.

The fixes hinges on three things:

  • PDO_SQLITE will create the database file if it doesn't exist.
  • Sqlite has a facility for dumping all SQL for generating and populating existing tables in a database file.
  • PHP_Shell allows you to interact with PHP at the command line.

So, here goes. First, create a SQL dump of your existing file. The sqlite command accepts two arguments: the database file to use, and either SQL or sqlite metacommands. In this case, we'll pass the command '.dump' (note the '.' prefix), and redirect output to a file:


sqlite mydata.db .dump > /tmp/mydata.sql
 

Now, we need to delete the existing database file, or back it up somewhere. Once done, we'll fire up PHP_Shell, using the php-shell.sh command (or php-shell.bat for Windows users). PHP_Shell is a handy utility that provides an interactive PHP shell, complete with history and completion. We'll use it to create our sqlite database file:

% phpshell 
PHP-Shell - Version 0.3.0, with readline() support
(c) 2006, Jan Kneschke 

>> use '?' to open the inline help 

>> $db = new PDO('sqlite:/path/to/mydata.db');
PDO::__set_state(array(
))
>> quit

With the database file created, we'll now load up that schema and data we dumped earlier. The nice part here is that the sqlite utility tends to be more tolerant of version differences, so we can load the data into the new database file using it, and PHP will be none the wiser:


sqlite mydata.db < /tmp/mydata.sql
 

All done!

Posted by Matthew Weier O'Phinney in PHP at 12:00 | Comments (0) | Trackbacks (0)

Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)

No comments

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back November '08 Forward
Mon Tue Wed Thu Fri Sat Sun
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Paul M. Jones
  • Mike Naberezny
  • Shahar Evron
  • Planet PHP
  • Zend Where I now work
  • Garden.org Where I once worked

Archives

November 2008
October 2008
September 2008
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml best practices
xml books
xml conferences
xml dojo
xml dpc08
xml file_fortune
xml linux
xml mvc
xml oop
xml pear
xml personal
xml php
xml phpworks08
xml programming
xml ubuntu
xml vim
xml webinar
xml zendcon
xml zendcon08
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net