SQLite stopped working after upgrade

9,046

Solution 1

Extensions

According to Vendor Specific Database Extensions page on the PHP site, there are two SQLite extensions:

SQLite3 extension is a newer replacement of SQLite, which soon will become obsolete:

The SQLite extension is enabled by default as of PHP 5.0.
Beginning with PHP 5.4, the SQLite extension is available only via PECL.

Ubuntu 10.04 vs 12.04

Now, some practicalities. Let's have a look at default web server configuration that was installed using:

sudo apt-get install apache2 php5 php5-sqlite

Ubuntu 10.04:

# php --ri sqlite

SQLite

SQLite support => enabled
PECL Module version => 2.0-dev
SQLite Library => 2.8.17
SQLite Encoding => UTF-8

# php --ri sqlite3

sqlite3

SQLite3 support => enabled
SQLite3 module version => 0.7-dev
SQLite Library => 3.6.22

Ubuntu 12.04:

# php --ri sqlite

Extension 'sqlite' not present.

# php --ri sqlite3

sqlite3

SQLite3 support => enabled
SQLite3 module version => 0.7-dev
SQLite Library => 3.7.9

So, Ubuntu 12.04 default PHP installation (as of writing this - version 5.3.10) already does not have sqlite extension as built in. The output above states that only sqlite3 extension is present, which came from php5-sqlite package.

Solution

Pick your evil:

  1. Right approach in long term would be to adapt the code to get it working under SQLite3. The changes would be minor, but this might involve some unwanted copy-paste-style work on a big amount of files. If this is the case, and right is not a right word here, solution 2 is for you.
  2. Install obsolete extension from PECL.

Solution 2

Following Andrejs Cainikovs' answer and strongly advising you to upgrade the code, here some examples of what kind of work should be done, when migrating from SQLiteDatabase class (sqlite library) to SQLite3 class (SQLite3 library).

For example:

  • SQLiteDatabase -> SQLite3,
  • SQLiteDatabase::unbufferedQuery -> SQLite3::query,
  • SQLiteResult::fetchAll(SQLITE_*) -> SQLite3Result::fetchArray(SQLITE3_*) etc.

As for fetching, in old SQLite we had:

$rowsIMEI = $db->unbufferedQuery($imeiSQL)->fetchAll(SQLITE_ASSOC);

foreach($rowsIMEI as $r)
{
    ...
}

While, in new SQLite3 we should:

$rowsIMEI = $db->query($imeiSQL);

while($r = $rowsIMEI->fetchArray(SQLITE3_ASSOC))
{
    ...
}

Other changes requires similar amount of work, so this shouldn't be a life-time process.

Before you begin, you have to first use a tool like SQLite Studio to convert your database file from 2.1 to 3.0. Size can be lowered by even a half, so I think it is something worth fighting for.

Share:
9,046

Related videos on Youtube

SteeveDroz
Author by

SteeveDroz

Updated on September 18, 2022

Comments

  • SteeveDroz
    SteeveDroz almost 2 years

    As many of you did, I upgraded from Lucid to Precise this week and almost everything seems to work find.

    everything except… SQLite, which was working well before but not anymore.

    I checked everything: phpinfo(), php.ini, sqlite3.ini.

    The sqlite command works fine, but when I try to use PHP... *PLOP*, nothing works.

    I get the message

    Fatal error: Class 'SQLiteDatabase' not found in /var/www/test/sqlite/index.php on line 4
    

    Line 4 being

    $db = new SQLiteDatabase('test.sqlite', 0666);
    

    Any hint?


    edit

    You can see my phpinfo() here: http://pastebin.com/jQ7Bz0GN

    The apache log is

     * Restarting web server apache2
    apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
    ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
    
  • SteeveDroz
    SteeveDroz about 12 years
    Thanks, but that's not it, I've got php5-sqlite installed correctly. Nice try, though!
  • Andrejs Cainikovs
    Andrejs Cainikovs about 12 years
    Have you defined your own SQLiteDatabase class? Asking this because for constructing SQLiteDatabase normally is being used this function - sqlite_open(). What will happen if you just replace SQLiteDatabase with sqlite_open?
  • SteeveDroz
    SteeveDroz about 12 years
    Hey! No, I didn't create my own SQLiteDatabase class.
  • Andrejs Cainikovs
    Andrejs Cainikovs about 12 years
    Then just try to replace SQLiteDatabase with sqlite_open.
  • SteeveDroz
    SteeveDroz about 12 years
    Doesn't work either: Fatal error: Call to undefined function sqlite_open().
  • Andrejs Cainikovs
    Andrejs Cainikovs about 12 years
    Well, in this case php5-sqlite is present on your system, but php won't load it. I would try with sudo apt-get install --reinstall php5-sqlite and then service apache2 restart. Another thing - can you please post somewhere phpinfo?
  • SteeveDroz
    SteeveDroz about 12 years
    I just tried, it's no good. Is php5-sqlite supposed to appear in the phpinfo? Because it doesn't.
  • Andrejs Cainikovs
    Andrejs Cainikovs about 12 years
    Yes, all PHP modules should appear there, including sqlite. Can you please post it somewhere, along with Apache logs when you restart server via service apache2 restart?
  • SteeveDroz
    SteeveDroz about 12 years
    I edited my question.
  • Andrejs Cainikovs
    Andrejs Cainikovs about 12 years
    Updated my answer. In short - your code should work. If it's not - restart your server and/or machine. Perhaps there were some misconfiguration issues during/after your system upgrade.
  • SteeveDroz
    SteeveDroz about 12 years
  • Andrejs Cainikovs
    Andrejs Cainikovs about 12 years
    Updated. Personally, I would recommend you to go with solution 1. Your first step would be to replace new SQLiteDatabase('test.sqlite', 0666); with new SQLite3('test.sqlite');, and then resolve all class incompatibilities.
  • Curious Apprentice
    Curious Apprentice about 12 years
    Is not it possible to remove sqlite an reinstall ?!.
  • SteeveDroz
    SteeveDroz about 12 years
    Thank you, thank you very much. The next error is Fatal error: Call to undefined method SQLite3::queryExec(), which is solved with what you said about class compatibility. @AndrejsCainikovs, you truely deserve your reward! Thanks for the long search.
  • Andrejs Cainikovs
    Andrejs Cainikovs about 12 years
    @Oltarus: I'm glad it helped. Welcome!
  • SteeveDroz
    SteeveDroz about 11 years
    Thank you. I figured it out. The one I still can't understand is why they changed SQLiteDatabase::singleQuery to SQLite3::querySingle, I'm still mad at them because I have to check every time, I'm unable to remember which is which.
  • trejder
    trejder about 11 years
    Oh, I believe that it is not them (the PHP team) who did the change. I think they've just wrote a new set of wrappers over new version of sqlite lib. You should go to sqlite.org to blame original SQLite's author! :]