Trace: • applications • utiliser-dao • 1.2.x • faq • 1.4
Differences ¶
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:tutorials:main:database-config [2007/09/16 15:13] – laurent | en:tutorials:main:database-config [2008/11/19 21:42] (current) – laurent | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | |||
+ | Before going further with code, you have to configure Jelix to be able to access a database, and feed this database a 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 news.org/ | ||
+ | |||
+ | Each profile has a name and are specified like this : | ||
+ | |||
+ | <code ini> | ||
+ | [ProfileName] | ||
+ | driver=" | ||
+ | database=" | ||
+ | host= " | ||
+ | user= " | ||
+ | password=" | ||
+ | persistent= on ; indicating if the connection is persistent | ||
+ | force_encoding = off; | ||
+ | </ | ||
+ | |||
+ | Except the driver parameter which is mandatory, the other parameters depends on the driver which are used. In general, you will have the database, host, user and password parameters. | ||
+ | |||
+ | Now change the content of this file according to your configuration. We will take as profile name " | ||
+ | |||
+ | <code ini> | ||
+ | default = news.org | ||
+ | |||
+ | [news.org] | ||
+ | driver=" | ||
+ | database=" | ||
+ | host= " | ||
+ | user= " | ||
+ | password=" | ||
+ | persistent= on | ||
+ | </ | ||
+ | |||
+ | |||
+ | Note: It is indeed sometimes interesting to have several entry points, then several settings sharing the same connection profiles file, or that each one has its own connections file. If you want to have different configuration files for the database access, create other ini files, and push their name in the **dbProfils** option in the configuration file of the entry point. | ||
+ | |||
+ | <code ini> | ||
+ | | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Creation of the table ===== | ||
+ | |||
+ | In the " | ||
+ | |||
+ | <code sql> | ||
+ | |||
+ | CREATE TABLE `news` ( | ||
+ | `news_id` INT NOT NULL AUTO_INCREMENT , | ||
+ | `subject` VARCHAR( 255 ) NOT NULL , | ||
+ | `text` TEXT NOT NULL , | ||
+ | `news_date` DATE NOT NULL , | ||
+ | PRIMARY KEY ( `news_id` ) | ||
+ | ); | ||
+ | </ | ||
+ | |||
+ | And feed this table with two news (we will make a form later). | ||
+ | |||
+ | <code sql> | ||
+ | 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.', | ||
+ | |||
+ | INSERT INTO `news` VALUES (2, 'Lorem Ipsum', | ||
+ | </ | ||
+ | |||
+ | We are now ready to use this data in our application. | ||