CodeIgniter 3 & SQLite3 on Windows Setup Guide
In this article
You can configure CodeIgniter 3 to use SQLite3 on Windows by enabling PHP's SQLite3 extensions in php.ini, creating a SQLite database file, and pointing CodeIgniter's database config at it with the SQLite3 driver. The whole job is mostly PHP configuration plus one config file edit, and it works on a standard XAMPP, WAMP, or manual PHP install. This guide walks through each step, then covers the mistakes that most often stop the connection from working.
What You'll Need
- A working CodeIgniter 3 installation on Windows (the framework files, typically under
htdocsorwww). - PHP 7.x or 8.x with the SQLite3 extension available. SQLite3 ships with PHP on Windows but is often commented out in
php.ini. - Write access to the folder where you plan to put the database file.
- A code editor and a browser. Optionally, a SQLite viewer such as DB Browser for SQLite to inspect the file.
Step-by-Step
- Find the active php.ini. The file PHP actually loads is shown in
phpinfo()under "Loaded Configuration File". In XAMPP it is usuallyC:\xampp\php\php.ini; in WAMP it is under the PHP version folder. Editing the wrong copy is the single most common reason changes appear to do nothing. - Enable the SQLite3 extensions. Open
php.iniand remove the leading semicolon from these lines so they read:extension=sqlite3extension=pdo_sqlite
Save the file. If your build uses absolute paths (some Windows builds do), the line may look likeextension=C:\path\to\php_sqlite3.dll; keep the path as it appears in the file, just remove the semicolon. Do not add lines that are not already present. - Restart the web server. Stop and start Apache (or the PHP-FPM service) so the new
php.iniis read. Reloadphpinfo()and confirm a "sqlite3" section now appears. If it does not, the extension did not load and nothing else will work. - Create the database file. SQLite stores a database as a single file. Create an empty file, for example
application/database/myapp.db, or let PHP create it on first connection. The folder must be writable by the web server user. On Windows this usually means the file is not read-only and the folder is not under a protected system path. - Configure the database settings. Open
application/config/database.phpand set the default group to use the SQLite3 driver. A working group looks like this:$db['default'] = array(
'dsn' => '',
'hostname' => '',
'username' => '',
'password' => '',
'database' => APPPATH.'database/myapp.db',
'dbdriver' => 'sqlite3',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
Thedatabasevalue must be an absolute path or a path built withAPPPATH/FCPATH; a bare filename can resolve to an unexpected working directory. - Load the database library. Either autoload it in
application/config/autoload.phpby adding'database'to thelibrariesarray, or load it in a controller with$this->load->database(). Without this step CodeIgniter will not open a connection. - Test the connection. In a controller method, run a simple query such as
$this->db->query('SELECT sqlite_version() AS v')and print the result. A version string confirms the driver, the file path, and the extension are all correct. - Create a table and verify persistence. Run a
CREATE TABLEstatement through CodeIgniter's query builder or$this->db->query(), then close the browser and reopen the page. If the table survives, the file is being written to the expected location. If it disappears, you are almost certainly writing to a different path than you think.
Common Mistakes
- Editing a different
php.inithan the one PHP loads. Always confirm viaphpinfo(). - Forgetting to restart Apache after changing
php.ini. - Using
dbdriver = 'sqlite'(the old PDO driver name) instead of'sqlite3'. CodeIgniter 3 expectssqlite3for the native driver. - Giving a relative database path. The file ends up in the server's working directory, not your project folder.
- Leaving the database folder read-only, or placing it somewhere the web server account cannot write, which produces a "unable to open database file" error.
- Assuming SQLite supports every MySQL feature. It has no
AUTO_INCREMENTkeyword (useINTEGER PRIMARY KEY), limitedALTER TABLE, and different date functions.
Tips
- Keep the database file outside the web root where possible, or at least deny direct HTTP access to the
.dbfile via your server config, so it cannot be downloaded. - Use
APPPATH.'database/name.db'so the path is stable regardless of where the project is deployed. - Back up the
.dbfile by copying it while no writes are happening; it is a single file, which makes backups trivial. - SQLite is fine for development and small sites. For concurrent writes from many users, a client-server database is the better fit.
- Keep
db_debugon during development so query errors are visible, and off in production.
Troubleshooting
If you see "could not find driver", the SQLite3 extension is not loaded: recheck php.ini, the extension line, and the server restart. If you see "unable to open database file", the path is wrong or the folder is not writable; test with an absolute path and check file permissions. If the connection succeeds but tables vanish between requests, print the resolved path with realpath() and compare it to the file you expect. If phpinfo() shows SQLite3 but CodeIgniter still fails, confirm you edited the correct database.php group and that the database library is actually loaded. Finally, check the CodeIgniter logs under application/logs for the exact error message before changing anything else.
Frequently Asked Questions
Do I need to install SQLite separately on Windows?
No. The SQLite3 extension is bundled with PHP on Windows; you only need to enable it in php.ini.
Which driver name should I use in database.php?
Use 'sqlite3' for the native driver. The older 'sqlite' value refers to a PDO-based driver and is not what most CodeIgniter 3 setups expect.
Can I use the same database file for development and production?
You can, but keep separate files so test data never reaches production, and make sure the production file is backed up and not web-accessible.
Why does my query work in a SQLite viewer but fail in CodeIgniter?
Usually a path or permission difference: the viewer opens the file you selected, while PHP opens the path in database.php. Print the resolved path to compare.
Does CodeIgniter 3 support SQLite migrations and the query builder?
Yes, the query builder works, but some schema operations differ from MySQL. Write migrations using SQLite-compatible SQL, such as INTEGER PRIMARY KEY instead of AUTO_INCREMENT.