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:43] – 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 25: | Ligne 25: | ||
Installation | Installation | ||
- | Bien que SQLite n'a atteint la notoriété que relativement récemment, ce projet a commencé | + | Bien que SQLite n'ait atteint la notoriété que relativement récemment, ce projet a commencé |
- | SQLite | + | SQLite |
sudo apt-get install sqlite3 libsqlite3-dev | sudo apt-get install sqlite3 libsqlite3-dev | ||
- | Maintenant, en tant qu'utilisateur | + | Maintenant, en tant que simple |
$ sqlite3 fullcircle.db | $ sqlite3 fullcircle.db | ||
Ligne 47: | Ligne 47: | ||
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 » - dans laquelle plusieurs champs sont définis pour chaque saisie. « id » sera simplement un code numérique d' | + | La commande CREATE TABLE crée une nouvelle table - appelée « issues » (numéros) |
sqlite> insert into issues values (110, 'issue 110', '- place pour la description -', ' | sqlite> insert into issues values (110, 'issue 110', '- place pour la description -', ' | ||
Ligne 87: | Ligne 87: | ||
sqlite> .quit** | sqlite> .quit** | ||
- | En la première, 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' | + | Dans la première |
Pour sortir du client en ligne de commande, nous tapons la commande : | Pour sortir du client en ligne de commande, nous tapons la commande : | ||
Ligne 101: | 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 116: | 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 132: | 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 142: | 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 157: | 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 168: | 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 176: | 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.1476340987.txt.gz · Dernière modification : 2016/10/13 08:43 de d52fr