issue113:tuto2
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
issue113:tuto2 [2016/10/13 08:09] – d52fr | issue113:tuto2 [2016/10/14 10:24] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 3: | Ligne 3: | ||
In the previous part of the series, we covered personalizing the Free Vision menu bar, responding to commands, and using default message boxes and dialogs. This third part will describe connecting our Free Pascal / Free Vision program with the Sqlite3 database.** | In the previous part of the series, we covered personalizing the Free Vision menu bar, responding to commands, and using default message boxes and dialogs. This third part will describe connecting our Free Pascal / Free Vision program with the Sqlite3 database.** | ||
- | Dans cette série d' | + | Dans cette série d' |
- | Dans la partie précédente de la série, nous avons couvert | + | Dans la partie précédente de la série, nous avons parlé de la personnalisation de la barre de menu Free Vision, la réponse aux commandes et l' |
**Installation | **Installation | ||
Ligne 11: | Ligne 11: | ||
Although SQlite achieved notoriety only relatively recently, the project began as far back as 2000. It certainly filled in an empty space in the universe of databases, since its big feature is that client programs link to a simple library. There is no need for installing a large database frontend on the user’s machine, nor to connect to an external database server over a network. Data is stored locally, in a simple file format. This makes it quite quick and suitable for small to medium-sized databases that are accessed basically by a single user, while at the same time making data easily transportable between computer systems (simply copy a file over). | Although SQlite achieved notoriety only relatively recently, the project began as far back as 2000. It certainly filled in an empty space in the universe of databases, since its big feature is that client programs link to a simple library. There is no need for installing a large database frontend on the user’s machine, nor to connect to an external database server over a network. Data is stored locally, in a simple file format. This makes it quite quick and suitable for small to medium-sized databases that are accessed basically by a single user, while at the same time making data easily transportable between computer systems (simply copy a file over). | ||
- | Sqlite | + | SQLite |
sudo apt-get install sqlite3 libsqlite3-dev | sudo apt-get install sqlite3 libsqlite3-dev | ||
Ligne 22: | Ligne 22: | ||
sqlite> create table issues (id integer primary key asc, title varchar, description varchar, download varchar, tags varchar); | sqlite> create table issues (id integer primary key asc, title varchar, description varchar, download varchar, tags varchar); | ||
sqlite> ** | sqlite> ** | ||
+ | |||
+ | Installation | ||
+ | |||
+ | Bien que SQLite n'ait atteint la notoriété que relativement récemment, ce projet a commencé dès 2000. Il a certainement rempli un vide dans l' | ||
+ | |||
+ | SQLite est probablement déjà installé sur un système Ubuntu, mais assurons-nous-en et installons en ligne de commande à la fois le client et le paquet pour développeur. Nous aurons besoin de cette seconde partie pour compiler l' | ||
+ | |||
+ | sudo apt-get install sqlite3 libsqlite3-dev | ||
+ | |||
+ | Maintenant, en tant que simple utilisateur, | ||
+ | |||
+ | $ sqlite3 fullcircle.db | ||
+ | SQLite version 3.11.0 2016-02-15 17:29:24 | ||
+ | Enter " | ||
+ | sqlite> create table issues (id integer primary key asc, title varchar, description varchar, download varchar, tags varchar); | ||
+ | sqlite> | ||
**The CREATE TABLE commands creates a new table - named “issues” - in which several fields are defined for each entry. “id” will simply be a numerical identification code that will be used by the system to index our entries in ascending mode. For each entry, information recorded will include the title, description, | **The CREATE TABLE commands creates a new table - named “issues” - in which several fields are defined for each entry. “id” will simply be a numerical identification code that will be used by the system to index our entries in ascending mode. For each entry, information recorded will include the title, description, | ||
Ligne 30: | Ligne 46: | ||
sqlite> insert into issues values (108, 'issue 108', '- description goes here -', ' | sqlite> insert into issues values (108, 'issue 108', '- description goes here -', ' | ||
+ | |||
+ | La commande CREATE TABLE crée une nouvelle table - appelée « issues » (numéros) - dans laquelle plusieurs champs sont définis pour chaque saisie. « id » sera simplement un code numérique d' | ||
+ | |||
+ | sqlite> insert into issues values (110, 'issue 110', '- place pour la description -', ' | ||
+ | |||
+ | sqlite> insert into issues values (109, 'issue 109', '- place pour la description -', ' | ||
+ | |||
+ | sqlite> insert into issues values (108, 'issue 108', '- place pour la description -', ' | ||
**Now, with some data inside the database, we can perform some queries: | **Now, with some data inside the database, we can perform some queries: | ||
Ligne 43: | Ligne 67: | ||
109 | 109 | ||
110** | 110** | ||
+ | |||
+ | Maintenant que nous avons des données dans la base, nous pouvons essayer quelques recherches : | ||
+ | |||
+ | sqlite> select * from issues where title=' | ||
+ | 109|issue 109|- place pour la description -|http:// | ||
+ | |||
+ | sqlite> select download from issues where id=110; | ||
+ | http:// | ||
+ | |||
+ | sqlite> select id from issues where tags like ' | ||
+ | 108 | ||
+ | 109 | ||
+ | 110 | ||
**In the first, we request all information for entries with title “issue 109”. In the second, we need just the download link for the entry with ID 110. In the last, we are searching for the IDs of all numbers that have been tagged with “inkscape” - note the use of “%” as wildcards to match any string that contains “inkscape”, | **In the first, we request all information for entries with title “issue 109”. In the second, we need just the download link for the entry with ID 110. In the last, we are searching for the IDs of all numbers that have been tagged with “inkscape” - note the use of “%” as wildcards to match any string that contains “inkscape”, | ||
Ligne 49: | Ligne 86: | ||
sqlite> .quit** | sqlite> .quit** | ||
+ | |||
+ | Dans la première recherche, nous demandons toutes les informations pour les enregistrements avec le titre « issue 109 ». Dans la seconde, nous voulons juste le lien de téléchargement pour la saisie contenant l' | ||
+ | |||
+ | Pour sortir du client en ligne de commande, nous tapons la commande : | ||
+ | |||
+ | sqlite> .quit | ||
**Interfacing with Free Pascal | **Interfacing with Free Pascal | ||
Ligne 58: | Ligne 101: | ||
uses | uses | ||
Crt, Classes, Strings, Sqlite3, Sqlite3db; | Crt, Classes, Strings, Sqlite3, Sqlite3db; | ||
+ | | ||
+ | L' | ||
+ | |||
+ | Nous commencerons par écrire un court programme, simplement pour tester la connectivité entre l' | ||
+ | |||
+ | Les membres du projet Free Pascal ont préparé une « unit » bien pratique. En fait, il y a plusieurs alternatives, | ||
+ | |||
+ | uses | ||
+ | Crt, Classes, Strings, Sqlite3, Sqlite3db; | ||
**We will need several variables to handle the connection. sql is the connection itself, while i and n will be used to iterate and to count the number of results returned by our query. res will format each line of results in a parseable manner. Though not really necessary, id and downloadURL will be used to hold the values of individual fields. | **We will need several variables to handle the connection. sql is the connection itself, while i and n will be used to iterate and to count the number of results returned by our query. res will format each line of results in a parseable manner. Though not really necessary, id and downloadURL will be used to hold the values of individual fields. | ||
Ligne 73: | Ligne 125: | ||
n := sql.List_Field.count; | n := sql.List_Field.count; | ||
+ | |||
+ | Nous aurons besoin de plusieurs variables pour gérer la connexion. sql est la connexion elle-même, tandis que i et n seront utilisés pour l' | ||
+ | |||
+ | var | ||
+ | sql : TSQLite; | ||
+ | i, n : Integer; | ||
+ | res : TStringList; | ||
+ | id, downloadURL : String; | ||
+ | | ||
+ | Nous commençons par la création d'une connexion, l' | ||
+ | |||
+ | sql := TSQLite.Create(dbfile); | ||
+ | sql.Query(dbquery, | ||
+ | |||
+ | n := sql.List_Field.count; | ||
**We can now iterate over the result lines: | **We can now iterate over the result lines: | ||
Ligne 89: | Ligne 156: | ||
The code for the complete program is available at this link: http:// | The code for the complete program is available at this link: http:// | ||
+ | |||
+ | Nous pouvons maintenant exécuter l' | ||
+ | |||
+ | for i := 1 to n do | ||
+ | begin | ||
+ | res := TStringList(sql.List_Field.items[i-1]); | ||
+ | id := res[0]; | ||
+ | downloadURL := res[1]; | ||
+ | Writeln (id, ' ', downloadURL); | ||
+ | end; | ||
+ | |||
+ | Enfin, même si SQLite est plutôt robuste, c'est toujours mieux de fermer la connexion proprement : | ||
+ | |||
+ | sql.Free; | ||
+ | |||
+ | Le code du programme complet est disponible par ce lien : http:// | ||
**Connecting Sqlite to Free Vision | **Connecting Sqlite to Free Vision | ||
Ligne 99: | Ligne 182: | ||
result := ExecuteDialog (pOpen, @FileName); | result := ExecuteDialog (pOpen, @FileName); | ||
+ | |||
+ | Connexion de SQLite à Free Vision | ||
+ | |||
+ | Dans la partie précédente de la série, nous avons conçu une application basique de Free Vision, avec un menu qui contenait un élément File > Open item (Fichier > Ouvrir un élément). Nous avons programmé cet élément pour sortir la commande cmOpen, qui était ensuite capturée dans la méthode HandleEvent pour créer un appel à l' | ||
+ | |||
+ | En premier lieu, nous modifierons le code créant TFileDialog de sorte qu'il filtre les fichiers avec l' | ||
+ | |||
+ | pOpen := New(PFileDialog, | ||
+ | |||
+ | result := ExecuteDialog (pOpen, @FileName); | ||
**After execution, result is an integer that holds either command cmOpen indicating the user closed the dialog using the “Open” button, or cmCancel if the “Cancel” button was used. FileName is a string with the file name chosen. We can now use this input to set up a Dialog window that creates a Sqlite connection, performs the query, and displays results: | **After execution, result is an integer that holds either command cmOpen indicating the user closed the dialog using the “Open” button, or cmCancel if the “Cancel” button was used. FileName is a string with the file name chosen. We can now use this input to set up a Dialog window that creates a Sqlite connection, performs the query, and displays results: | ||
Ligne 114: | Ligne 207: | ||
ExecuteDialog (pDisplay, nil); | ExecuteDialog (pDisplay, nil); | ||
end;** | end;** | ||
+ | |||
+ | Après exécution, le résultat est un entier qui contient soit la commande cmOpen indiquant que l' | ||
+ | |||
+ | if not (result = cmCancel) then | ||
+ | |||
+ | begin | ||
+ | GetExtent(R); | ||
+ | R.A.X := R.A.X + 2; | ||
+ | R.A.Y := R.A.Y + 2; | ||
+ | R.B.X := R.B.X - 2; | ||
+ | R.B.Y := R.B.Y - 2; | ||
+ | pDisplay := New(PDisplaySQLDialog, | ||
+ | |||
+ | ExecuteDialog (pDisplay, nil); | ||
+ | end; | ||
**R is a variable of type TRect that Free Vision uses to indicate a rectangular region on screen. The two fields A and B are the top-left and bottom-right positions occupied that define the rectangle, which is in this case X and Y the column and row numbers. On the other hand, pDisplay is a pointer to a TDisplaySQLDialog - an object that inherits from TDialog and is not part of the standard Vision libraries. So let us define it, overwriting only the constructor Init method to pass along the database file name to be opened and displayed: | **R is a variable of type TRect that Free Vision uses to indicate a rectangular region on screen. The two fields A and B are the top-left and bottom-right positions occupied that define the rectangle, which is in this case X and Y the column and row numbers. On the other hand, pDisplay is a pointer to a TDisplaySQLDialog - an object that inherits from TDialog and is not part of the standard Vision libraries. So let us define it, overwriting only the constructor Init method to pass along the database file name to be opened and displayed: | ||
Ligne 125: | Ligne 233: | ||
R.Assign(0, 0, 78, 17); | R.Assign(0, 0, 78, 17); | ||
inherited Init (R, ' | inherited Init (R, ' | ||
+ | |||
+ | R est une variable de type TRect que Free Vision utilise pour indiquer une région rectangulaire de l' | ||
+ | |||
+ | TDisplaySQLDialog = object(TDialog) | ||
+ | constructor Init (FileName : String); | ||
+ | end; | ||
+ | |||
+ | Avoir un Init personnalisé sert deux objectifs : c'est ici que sera construit l' | ||
+ | |||
+ | R.Assign(0, 0, 78, 17); | ||
+ | inherited Init (R, ' | ||
**Now, let us fit in the Sqlite routine to populate a PStringCollection with the results of our query. Unfortunately, | **Now, let us fit in the Sqlite routine to populate a PStringCollection with the results of our query. Unfortunately, | ||
Ligne 133: | Ligne 252: | ||
In this part, we set up a small Sqlite database, then built a command-line Free Pascal program to access it. Finally, we integrated the database code into our Free Vision application through a new Dialog type to connect to the database and display retrieved data. In the next part of the series, we will connect to the Internet in order to refresh the information in our database directly from the Full Circle Magazine website.** | In this part, we set up a small Sqlite database, then built a command-line Free Pascal program to access it. Finally, we integrated the database code into our Free Vision application through a new Dialog type to connect to the database and display retrieved data. In the next part of the series, we will connect to the Internet in order to refresh the information in our database directly from the Full Circle Magazine website.** | ||
+ | |||
+ | Maintenant, finissons la routine SQLite pour remplir PStringCollection avec les résultats de notre recherche. Malheureusement, | ||
+ | |||
+ | En ayant fait ceci, nous avons paramétré la fenêtre de dialogue et rempli les Items (éléments) avec les données récupérées dans la base. Ce n'est plus maintenant qu'une simple question de paramétrage des gadgets et d' | ||
+ | |||
+ | C'est à peu près tout. Le code complet de notre programme, jusqu' | ||
+ | |||
+ | Dans cette partie, nous avons paramétré une petite base de données SQLite, puis construit un programme Free Pascal en ligne de commande pour y accéder. Enfin, nous avons intégré le code de la base de données dans notre application Free Vision par l' | ||
issue113/tuto2.1476338990.txt.gz · Dernière modification : 2016/10/13 08:09 de d52fr