CodeIgniter 3でSQLite3を使用する(Debian 7 + php-fpm)
CodeIgniter 3で、データベースの使い方を調べようと思います。お手軽に使用できるデータベースとして、SQLite3を使ってみようと思います。
http://www.sqlite.org/
接続ライブラリのインストール
SQLiteを使用するため、ライブラリのインストールを行います。
これをインストールしていないと、こんなエラーが発生します。
Fatal error: Class 'SQLite3' not found in
/var/www/ci3/system/database/drivers/sqlite3/sqlite3_driver.php on line 89
apt-getでインストールしました。
# apt-get -y install php5-sqlite
database.php
SQLiteを使用するためのdatabase.phpの記載はこのようになりました。
- $active_group = 'default';
- $query_builder = TRUE;
- $db['default'] = array(
- 'dsn' => '',
- 'hostname' => '',
- 'username' => '',
- 'password' => '',
- //'database' => '/var/www/ci3/application/sqlite3/test.db',
- 'database' => APPPATH.'sqlite3/test.db',
- 'dbdriver' => 'sqlite3',
- 'dbprefix' => '',
- 'pconnect' => FALSE,
- 'db_debug' => TRUE,
- 'cache_on' => FALSE,
- 'cachedir' => '',
- 'char_set' => '',
- 'dbcollat' => '',
- 'swap_pre' => '',
- 'autoinit' => TRUE,
- 'encrypt' => FALSE,
- 'compress' => FALSE,
- 'stricton' => FALSE,
- 'failover' => array(),
- 'save_queries' => TRUE
- );
最低限必要なのは、「database」の項目で、ここにデータベースファイルのパスを指定します。
最初、ファイルの実体は存在しなくてOKです。
この設定ファイル中で、APPPATHのようなCodeIgniterの定数も使用できます。
こんなコントローラーを作成。
- <?php
- class Sample extends CI_Controller {
- public function index() {
- $this->load->database();
- }
- }
http://[サーバーIP]/sample/をブラウザで表示すると、
同時にデータベースファイルが作成されました。

マイグレーションによるテーブル作成
せっかくなので、マイグレーション機能を使用してテーブルを作成してみます。
データベースのマイグレーション(Library Reference - Migrations Class)
application/config/migration.phpを編集。
2箇所設定を変更します。
- $config['migration_enabled'] = TRUE;
- $config['migration_version'] = 20150217000000;
application/migrationsに20150217000000_my_table.phpを作成。
CI_Migrationを継承し、クラス名は
[Migration_] + ファイル名の日付以降。今回だと[My_Table]とします。
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class Migration_My_Table extends CI_Migration {
- public function up() {
- // テーブル作成
- $this->dbforge->add_field(array(
- 'name' => array(
- 'type' => 'TEXT'
- ),
- 'title' => array(
- 'type' => 'TEXT'
- ),
- 'email' => array(
- 'type' => 'TEXT'
- ),
- ));
- $this->dbforge->create_table('my_table');
- // 初期データを登録
- $data = array(
- 'name' => '西住 みほ',
- 'title' => 'あんこうチーム',
- 'email' => 'miho@example.com',
- );
- $this->db->insert('my_table', $data);
- $data = array(
- 'name' => '小山 柚子',
- 'title' => 'カメさんチーム',
- 'email' => 'yuzu@example.com',
- );
- $this->db->insert('my_table', $data);
- }
- public function down() {
- $this->dbforge->drop_table('my_table');
- }
- }
マイグレーションを実行するコントローラーを作成。
- <?php
- class Migrate extends CI_Controller
- {
- public function index()
- {
- $this->load->library('migration');
- if ($this->migration->current() === FALSE)
- {
- show_error($this->migration->error_string());
- }
- }
- }
コンソールで、CodeIgniterのカレントに移動し、以下のコマンドを実行。
※ブラウザでhttp://[サーバーIP]/migrateを表示してもOK
# php5 index.php migrate
実行すると、sqliteのデータベースファイルのサイズが大きくなりました。
どうやらちゃんとテーブルの作成ができたようです。
CodeIgniter 3のユーザーガイド(User Guide)まとめ
- 関連記事
-
- CodeIgniter 3 データベースの検索結果取得について(Database Reference - Generating Query Results)
- CodeIgniter 3 データベースの接続サンプル(Database Reference - Quick Start: Usage Examples)
- CodeIgniter 3でSQLite3を使用する(Debian 7 + php-fpm)
- CodeIgniter 3 ユニットテストをコマンドで実行する
- CodeIgniter 3 ユニットテストクラス(Library Reference - Unit Testing Class)
コメント