Configure Codeigniter 3.1 and SQLite3 in Windows

Introduction

Here in this tutorial I will show you how to configure Codeigniter 3.1 and SQLite3 in Windows environment. You can choose any database from a number of supported databases in Codeigniter framework but here I am going to use SQLite, which is in-memory database and also fit for quick PoC projects.

SQLite a small, fast, self-contained, high-reliability, full-featured, SQL database engine. SQLite is the most used database engine in the world. SQLite is built into all mobile phones and most computers and comes bundled inside countless other applications. SQLite file format is stable, cross-platform, and backwards compatible.

Prerequisites

PHP 7.4.3, Apache 2.4, Codeigniter 3.1.11

Configure Codeigniter and SQLite3

Config – database.php

Open file <project root directory>/application/config/database.php file and update the database and dbdriver keys:

...
'database' => APPPATH.'db/roytuts.sqlite',
'dbdriver' => 'sqlite3',
...

In the above configuration, database key finds the SQLite3 database file in the specified path. SQLite3 is the in-memory database, so it’s a database file, for example, roytuts.sqlite and kept under <project root directory>/application/db directory.

If your database file does not exists under the desired folder then the database file will be created with 0 KB.

The dbdriver key finds the database driver. We have given here sqlite3. You don’t need to provide this driver to the Codeigniter framework manually. The SQLite3 driver comes with the Codeigniter framework package. You will find sqlite3 driver under <project root directory>/system/database/drivers.

Config – autoload.php

Generally we auto-load things, which are required
to uniformly work throughout the application instead of loading again and again.

Therefore, we will auto-load our database so that it could be loaded once and used throughout the application.

$autoload['libraries'] = array('database');

The above configuration will load database during application startup and if there is any error, while loading, it will be shown to you.

Testing the Database Connection

Let’s say the file roytuts.sqlite does not exist under the given path
<project root directory>/application/db and when you try to access the application into browser by hitting URL http://localhost/<project root directory>/index.php, then roytuts.sqlite file will be created with 0 KB in size. If the file already exists in the desired directory then it will be loaded instead of creating the new file.

If you do not see any error on the browser, it means you have successfully configured Codeigniter 3.1 and SQLite3 in Windows environment.

Thanks for reading.

1 thought on “Configure Codeigniter 3.1 and SQLite3 in Windows

Leave a Reply

Your email address will not be published. Required fields are marked *