Also available in these languages:
English日本語polskiРусский简体中文

Setting Up Database

Having created a skeleton application and finished the database design, in this section we will create the blog database and establish the connection to it in the skeleton application.

Creating Database

We choose to create a SQLite database. Because the database support in Yii is built on top of PDO, we can easily switch to use a different type of DBMS (e.g. MySQL, PostgreSQL) without the need to change our application code.

We create the database file blog.db under the directory /wwwroot/blog/protected/data. Note that both the directory and the database file have to be writable by the Web server process, as required by SQLite. We may simply copy the database file from the blog demo in our Yii installation which is located at /wwwroot/yii/demos/blog/protected/data/blog.db. We may also generate the database by executing the SQL statements in the file /wwwroot/yii/demos/blog/protected/data/schema.sqlite.sql.

Tip: To execute SQL statements, we may use the sqlite3 command line tool that can be found in the SQLite official website.

Establishing Database Connection

To use the blog database in the skeleton application we created, we need to modify its application configuration which is stored in the PHP script /wwwroot/blog/protected/config/main.php. The script returns an associative array consisting of name-value pairs, each of which is used to initialize a writable property of the application instance.

We configure the db component as follows,

return array(
    ......
    'components'=>array(
        ......
        'db'=>array(
            'connectionString'=>'sqlite:/wwwroot/blog/protected/data/blog.db',
            'tablePrefix'=>'tbl_',
        ),
    ),
    ......
);

The above configuration says that we have a db application component whose connectionString property should be initialized as sqlite:/wwwroot/blog/protected/data/blog.db and whose tablePrefix property should be tbl_.

With this configuration, we can access the DB connection object using Yii::app()->db at any place in our code. Note that Yii::app() returns the application instance that we create in the entry script. If you are interested in possible methods and properties that the DB connection has, you may refer to its class reference. However, in most cases we are not going to use this DB connection directly. Instead, we will use the so-called ActiveRecord to access the database.

We would like to explain a bit more about the tablePrefix property that we set in the configuration. This tells the db connection that it should respect the fact we are using tbl_ as the prefix to our database table names. In particular, if in a SQL statement there is a token enclosed within double curly brackets (e.g. {{post}}}, then the db connection should translate it into a name with the table prefix (e.g. tbl_post) before sending it to DBMS for execution. This feature is especially useful if in future we need to modify the table name prefix without touching our source code. For example, if we are developing a generic content management system (CMS), we may exploit this feature so that when it is being installed in a new environment, we can allow users to choose a table prefix they like.

Tip: If you want to use MySQL instead of SQLite to store data, you may create a MySQL database named blog using the SQL statements in /wwwroot/yii/demos/blog/protected/data/schema.mysql.sql. Then, modify the application configuration as follows,

return array(
    ......
    'components'=>array(
        ......
        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=blog',
            'emulatePrepare' => true,
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
            'tablePrefix' => 'tbl_',
        ),
    ),
  ......
);
$Id: prototype.database.txt 1677 2010-01-07 20:29:26Z qiang.xue $
If you find any typos or errors in the tutorial, please create a Yii ticket to report it. If it is a translation error, please create a Yiidoc ticket, instead. Thank you.

Total 9 comments:

#209
Use this for MySQL
by Q-efx at 4:38am on April 18, 2009.

array( ...... 'components'=>array( ...... 'db'=>array( 'class'=>'CDbConnection', 'connectionString'=>'mysql:host=localhost;dbname=testdb', 'username'=>'root', 'password'=>'password', ), ), )

#244
Database schema import file for MSSQL & MYSQL
by tim at 4:12pm on April 28, 2009.

Check this folder in your installation for database schema import files for MSSQL & MYSQL:

/wwwroot/yii/demos/blog/protected/data/

#260
sqlite
by joemite at 5:43pm on May 4, 2009.

If you're like me and have never used sqlite before...

sqlite3 /wwwroot/blog/protected/data/blog.db

SQLite version 3.4.2 Enter ".help" for instructions sqlite> .read /wwwroot/yii/demos/blog/protected/data/schema.sqlite.sql sqlite> .quit

#374
DB declaration commented by default
by foxb at 6:16pm on June 11, 2009.

Be aware that db declaration is commented out by default.

If you do not uncomment you will have exception at next step.

#598
Sqlite DSN on windows nder IIS
by aimtech at 8:20pm on August 19, 2009.

If you are using windows and IIS, the connection string for sqlite should be like this:

'db'=>array( 'class'=>'CDbConnection', 'connectionString'=>'sqlite:C:\Inetpub\wwwroot\blog\protected\data\blog.db', ),

#741
Possible problems with PDO
by 7aco at 10:59am on October 17, 2009.

for me: PHP Version 5.3.0 PDO Driver for MySQL enabled Client API version mysqlnd 5.0.5-dev - 081106 - $Revision: 1.3.2.27 $ specifying dns like : mysql:host=localhost;dbname=testdb failed creating models (PDO complained about some invalid argument). Solution: either specify host as 127.0.0.1 or add socket info to dns: mysql:host=localhost;dbname=testdb;unix_socket=/tmp/mysql.sock info about the socket location can be found in your my.cnf file

#1275
SQL Code
by Mrsome1 at 3:07am on March 15, 2010.

Could you please post here the SQL code for blog database?

It will help us to import it in our DB.

#1617
Help ! I try to create blog db...
by Sart at 6:02am on June 23, 2010.

You wrote: "We may also generate the database by executing the SQL statements in the file /wwwroot/yii/demos/blog/protected/data/schema.sqlite.sql" I try so: sqlite>.read schema.sqlite.sql No any base is created ...but file is found and opened.. What is the matter ?

#1785
Checking password in the DB
by nbezzala at 8:49am on August 13, 2010.

I want to use the MySQL PASSWORD function to check if the user has entered the correct password, instead of using the md5 function.

How do I do this?

Your Comment:

You may enter comment using Markdown syntax.

Please login with your forum account.
Note: you must have at least ONE forum post with your account.