issue113:tuto2
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
issue113:tuto2 [2016/10/03 11:08] – créée auntiee | issue113:tuto2 [2016/10/14 10:24] (Version actuelle) – auntiee | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | In this series of articles, I will be building a text-based application with Free Pascal, using its text-based interface for user interaction. This will be combined with other, more modern, technologies such as database access using SQL and Web access with HTTP. The final aim of the project is to demonstrate how Pascal can be used to build a modern application, | + | **In this series of articles, I will be building a text-based application with Free Pascal, using its text-based interface for user interaction. This will be combined with other, more modern, technologies such as database access using SQL and Web access with HTTP. The final aim of the project is to demonstrate how Pascal can be used to build a modern application, |
- | 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.** |
- | Installation | + | Dans cette série d' |
+ | |||
+ | 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 | ||
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 17: | Ligne 21: | ||
Enter " | Enter " | ||
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> |
- | 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, | + | 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, | ||
sqlite> insert into issues values (110, 'issue 110', '- description goes here -', ' | sqlite> insert into issues values (110, 'issue 110', '- description goes here -', ' | ||
Ligne 25: | Ligne 45: | ||
sqlite> insert into issues values (109, 'issue 109', '- description goes here -', ' | sqlite> insert into issues values (109, 'issue 109', '- description goes here -', ' | ||
- | 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: |
sqlite> select * from issues where title=' | sqlite> select * from issues where title=' | ||
109|issue 109|- description goes here -|http:// | 109|issue 109|- description goes here -|http:// | ||
+ | |||
+ | sqlite> select download from issues where id=110; | ||
+ | http:// | ||
+ | |||
+ | sqlite> select id from issues where tags like ' | ||
+ | 108 | ||
+ | 109 | ||
+ | 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; | sqlite> select download from issues where id=110; | ||
Ligne 40: | Ligne 81: | ||
110 | 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”, |
To exit the command line client, we issue command: | To exit the command line client, we issue command: | ||
+ | |||
+ | 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 | sqlite> .quit | ||
- | Interfacing with Free Pascal | + | **Interfacing with Free Pascal |
We will begin by writing a short program, just to test connectivity between our program environment and the Sqlite library. In it, we will limit ourselves to performing a quick search and reporting results on-screen with the Writeln command. | We will begin by writing a short program, just to test connectivity between our program environment and the Sqlite library. In it, we will limit ourselves to performing a quick search and reporting results on-screen with the Writeln command. | ||
Free Pascal project members have prepared a suitable unit. There are actually several alternatives, | Free Pascal project members have prepared a suitable unit. There are actually several alternatives, | ||
+ | |||
+ | uses | ||
+ | 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 | uses | ||
Crt, Classes, Strings, Sqlite3, Sqlite3db; | 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. |
var | var | ||
Ligne 64: | Ligne 120: | ||
We begin by creating a connection, executing our query and counting the number of lines in the result: | We begin by creating a connection, executing our query and counting the number of lines in the result: | ||
+ | |||
+ | sql := TSQLite.Create(dbfile); | ||
+ | sql.Query(dbquery, | ||
+ | |||
+ | 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 := TSQLite.Create(dbfile); | ||
Ligne 70: | Ligne 141: | ||
n := sql.List_Field.count; | n := sql.List_Field.count; | ||
- | We can now iterate over the result lines: | + | **We can now iterate over the result lines: |
for i := 1 to n do | for i := 1 to n do | ||
Ligne 84: | Ligne 155: | ||
sql.Free; | sql.Free; | ||
- | The code for the complete program is available at this link: http:// | + | The code for the complete program is available at this link: http:// |
- | Connecting Sqlite to Free Vision | + | 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 | ||
In the previous part of this series, we designed a basic Free Vision application, | In the previous part of this series, we designed a basic Free Vision application, | ||
In the first place, we will modify the code creating the TFileDialog so it filters for files with the .db extension: | In the first place, we will modify the code creating the TFileDialog so it filters for files with the .db extension: | ||
+ | |||
+ | pOpen := New(PFileDialog, | ||
+ | |||
+ | 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, | pOpen := New(PFileDialog, | ||
Ligne 96: | Ligne 193: | ||
result := ExecuteDialog (pOpen, @FileName); | 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: |
+ | |||
+ | 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;** | ||
+ | |||
+ | Après exécution, le résultat est un entier qui contient soit la commande cmOpen indiquant que l' | ||
if not (result = cmCancel) then | if not (result = cmCancel) then | ||
Ligne 111: | Ligne 223: | ||
end; | 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: |
TDisplaySQLDialog = object(TDialog) | TDisplaySQLDialog = object(TDialog) | ||
Ligne 118: | Ligne 230: | ||
Having a tailored Init will serve two purposes: it is here that we will build up the interface, with a PListBox to hold the data to be displayed, a PScrollBar to allow us to move up and down in the list, and a PButton to close the dialog window. Let us start by setting up the window size and title using the inherited TDialog constructor method: | Having a tailored Init will serve two purposes: it is here that we will build up the interface, with a PListBox to hold the data to be displayed, a PScrollBar to allow us to move up and down in the list, and a PButton to close the dialog window. Let us start by setting up the window size and title using the inherited TDialog constructor method: | ||
+ | |||
+ | R.Assign(0, 0, 78, 17); | ||
+ | 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); | R.Assign(0, 0, 78, 17); | ||
inherited Init (R, ' | 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, |
With this done, we have the dialog window set up and Items populated with the data retrieved from the database. It is now a simple matter of setting up the widgets and inserting each into the Dialog window (code is bottom right). | With this done, we have the dialog window set up and Items populated with the data retrieved from the database. It is now a simple matter of setting up the widgets and inserting each into the Dialog window (code is bottom right). | ||
Ligne 128: | Ligne 251: | ||
That’s about it. The complete code for our program, so far, is to be found at link: http:// | That’s about it. The complete code for our program, so far, is to be found at link: http:// | ||
- | 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.1475485719.txt.gz · Dernière modification : 2016/10/03 11:08 de auntiee