Trace: • database-config
Chapter: Configuration of the database
« Creating a first simple action | How to use a DAO » |
This is an old revision of the document!
Table of Contents
Configuring the database access ¶
Before going further with code, you have to configure Jelix to be able to access a database, and feed this databasea little bit. We will indeed store our news in a table
Jelix can handle mysql, postgresql, sqlite and pdo. Through its driver system, it is possible to add other types of databases (if you don't want to use pdo).
Access configuration file ¶
The access to a database is configured in the actu.org/var/config/dbprofils.ini.php file. In this file, you can specify one or several connexion profiles, then one ore several connexions.
Each profile has a name and is specified like this :
[ProfileName] driver="mysql" ; name of the driver to use database="foo" ; name of the base to use host= "localhost" ; name of the machine of the database server user= "john" ; connexion user password="doo" ; password persistent= on ; indicating if the connexion is persistent
Except the dirver parameter, which is mandatory, the other parameters depend on the driver which is used. In general, you will have the database, host, user and password parameters. Change the content of this file according to your configuration. We will take as profile name “actu.org” for example, and we will define this profile as the default one by specifying it with the “default” parameter.
default = actu.org [actu.org] driver="mysql" database="actu" host= "localhost" user= "actu" password="actu" persistent= on
Check in the actu.org/var/config/config.classic.ini.php file that the dbprofil parameter shows the dbprofils.ini.php clearly.
dbProfils = dbprofils.ini.php
It is indeed sometimes interesting to have several entrance points, then several settings sharing the same connexion profiles file, or that each one has its own connexions file.
Creation of the table ¶
In the “actu” base, we will now create a news table. Execute this sql script (adapt it if you are not using mysql):
CREATE TABLE `news` ( `id_news` INT NOT NULL AUTO_INCREMENT , `subject` VARCHAR( 255 ) NOT NULL , `text` TEXT NOT NULL , `news_date` DATE NOT NULL , PRIMARY KEY ( `id_news` ) );
And feed this table avec deux news (we will make a form later).
INSERT INTO `news` VALUES (1, 'first news', 'This is a first news. In commodo, neque sit amet laoreet accumsan, neque velit rutrum augue, a fringilla nibh lorem nec est. Cras eleifend eros. Sed vehicula. Donec vel enim at nunc tincidunt pellentesque. Donec malesuada. Praesent volutpat orci ut leo. Donec dictum tortor quis odio. Aliquam pulvinar justo eu eros.', '2006-01-15'); INSERT INTO `news` VALUES (2, 'Lorem Ipsum', 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse enim elit, luctus in, rhoncus quis, facilisis in, nulla. Nam eu dolor vel erat semper porta. Phasellus pellentesque nulla a urna. Phasellus nonummy diam id risus. Donec faucibus mi sed nisi. Sed et lectus at ligula scelerisque tempus. Proin justo nibh, consectetuer porta, accumsan ac, consectetuer id, dui. Morbi at mi auctor urna elementum convallis. Etiam et massa porta risus imperdiet ullamcorper. Aenean a metus at tortor ultrices accumsan. Mauris luctus.', '0000-00-00');
We are now ready to use this data in our application.
- Next : Using a DAO
- Previous : Creating an action