Mais le code étant déjà un peu lourd, et le manque d'exemples d'utilisation de SQLite sur le forum etant ce qu'il est, ça m'a donné envie de poster ça.
Le code n'est pas forcément des plus optimisés mais reste fonctionnel. Encore une fois c'est pour l'exemple.
Vous pouvez :
- Créer plusieurs projets (bases)
- Ajouter des contacts
- Effacer un contact
- Modifier un contact
EnJoYeZ
Code : Tout sélectionner
UseSQLiteDatabase()
5
; ------------------------------
; SQLite Demo par Ar-S - 05/2022
; Create Database
; Delete Database
; Open Database
; Update Database
; ------------------------------
Enumeration
#WIN
#MaBase
#Fichier
#Liste
#Info
#NOM
#PRENOM
#TEL
#ADRESSE
#CP
#VILLE
#EMAIL
#BT_AJOUTER
#BT_ANNULER_AJOUT
#BT_MODIFIER
#BT_ANNULER_MODIF
#BT_SUPPR
#Menu
#M_Creer
#M_Ouvrir
#M_Fermer
EndEnumeration
#ASCENDANT = 0
#DESCENDANT = 1
;ETAT Modifier
#ECRITURE = 0
#VALIDATION = 1
;ETAT Ajouter
#OUVERT = 1
#FERMER = 0
Global Filename$ = GetCurrentDirectory()
Global N = -1
Global.b trie = #ASCENDANT, modifier = #ECRITURE, ajouter = #fermer
Declare CreateBase()
Declare.s OpenProject()
Declare MyMenu()
Declare LectureBDD(BaseID, NomColonne$="prenom", ordre=0)
Declare Insere(Base, prenom$, nom$, email$, tel$, ADRESSE$, cp$, VILLE$)
Declare Delete(Base, id)
Declare.s AfficheInfo(base, numID)
Declare Alert(message$, color=#Red)
Structure Info
id.i
nom.s
prenom.s
email.s
tel.s
adresse.s
cp.s
ville.s
EndStructure
Global NewList Info.Info()
Macro AddClientList(_id,_prenom,_nom,_email,_tel,_adresse,_cp,_ville)
AddElement( info() )
info()\ID = Val(_id)
info()\prenom = _prenom
info()\nom = _nom
info()\email = _email
info()\tel = _tel
info()\adresse = _adresse
info()\cp = _cp
info()\ville = _ville
EndMacro
; PROCEDURE Pour les gens ne connaissant pas les requêtes SQL
Procedure Insere(Base, prenom$, nom$, email$, tel$, ADRESSE$, cp$, VILLE$)
SetGadgetColor(#info,#PB_Gadget_FrontColor, #Green)
SetDatabaseString(Base, 0, prenom$)
SetDatabaseString(Base, 1, nom$)
SetDatabaseString(Base, 2, email$)
SetDatabaseString(Base, 3, tel$)
SetDatabaseString(Base, 4, ADRESSE$)
SetDatabaseString(Base, 5, CP$)
SetDatabaseString(Base, 6, VILLE$)
DatabaseUpdate(Base, "INSERT INTO contacts (prenom, nom, email, tel, adresse, cp, ville) VALUES (?, ?, ?, ?, ?, ?, ?);")
If Result = 0
erreur$ = DatabaseError()
If FindString(erreur$,"UNIQUE")
POS = FindString (erreur$,"contacts.") : POS+9
Colonne$ = Mid(erreur$,POS, Len(erreur$)-POS)
Alert(email$+ " déjà présent dans la base")
Else
Alert (DatabaseError() )
EndIf
Else
Alert(PRENOM$ + " " + NOM$ + " ajouté !",#Green)
SetGadgetText(#PRENOM, "") : SetGadgetText(#NOM, "") : SetGadgetText(#EMAIL, "")
EndIf
ProcedureReturn Result
EndProcedure
Procedure Update(Base, prenom$, nom$, email$, tel$, ADRESSE$, cp$, VILLE$)
SetGadgetColor(#info,#PB_Gadget_FrontColor, #Green)
SetDatabaseString(Base, 0, PRENOM$)
SetDatabaseString(Base, 1, NOM$)
SetDatabaseString(Base, 2, email$)
SetDatabaseString(Base, 3, tel$)
SetDatabaseString(Base, 4, ADRESSE$)
SetDatabaseString(Base, 5, CP$)
SetDatabaseString(Base, 6, VILLE$)
SetDatabaseString(Base, 7, Str(info()\id))
Result = DatabaseUpdate(Base, "UPDATE contacts SET prenom = ?, nom = ?, email = ?, tel = ?, adresse = ?, cp = ?, ville = ? WHERE id = ?")
If Result = 0
erreur$ = DatabaseError()
Alert (DatabaseError() )
Else
Alert(PRENOM$ + " " + NOM$ + " modifié !",#Green)
EndIf
ProcedureReturn Result
EndProcedure
Procedure Delete(Base, id)
Protected R
R = MessageRequester("Confirmation","Souhaitez vous réellement supprimer le contact "+info()\nom + " " + info()\prenom + " ?",#PB_MessageRequester_YesNo|#MB_ICONQUESTION)
Select R
Case #PB_MessageRequester_Yes
Result = DatabaseUpdate(Base, "DELETE FROM contacts WHERE id="+id)
If Result = 0
erreur$ = DatabaseError()
Alert (erreur$)
Else
Alert(info()\nom + " " + info()\prenom +" a été supprimé !",#Red)
EndIf
Case #PB_MessageRequester_No
Result = 0
Default
Result = 0
EndSelect
ProcedureReturn Result
EndProcedure
Procedure.s AfficheInfo(base, numID)
Shared match$
SetGadgetColor(#info,#PB_Gadget_FrontColor, #Green)
Requete$ = "SELECT * FROM contacts WHERE id ="+numID+" LIMIT 1"
If DatabaseQuery(Base, Requete$)
While NextDatabaseRow(base) ; Enumeration des enregistrements
If GetDatabaseString(Base, 0) = Str(numID)
Debug "Trouvé !"
match$ = "ID:"+GetDatabaseString(Base, 0) + " - " + GetDatabaseString(Base, 1) + " " + GetDatabaseString(Base, 2)
SetGadgetText(#PRENOM,GetDatabaseString(Base, 1))
SetGadgetText(#NOM,GetDatabaseString(Base, 2))
SetGadgetText(#EMAIL,GetDatabaseString(Base, 3))
SetGadgetText(#TEL,GetDatabaseString(Base, 4))
SetGadgetText(#ADRESSE,GetDatabaseString(Base, 5))
SetGadgetText(#CP,GetDatabaseString(Base, 6))
SetGadgetText(#VILLE,GetDatabaseString(Base, 7))
EndIf
Wend
FinishDatabaseQuery(base)
Else
match$ = DatabaseError()
EndIf
ProcedureReturn match$
EndProcedure
;**********************************
; GESTION DE LA BASE
;**********************************
Procedure CreateBase()
If OpenDatabase(#MaBase, Filename$, "", "")
Requete$ = "CREATE TABLE contacts (id INTEGER PRIMARY KEY, prenom VARCHAR(50) Not NULL, nom VARCHAR(50) Not NULL, email VARCHAR(100) NULL UNIQUE, tel VARCHAR (10) Not NULL UNIQUE, adresse VARCHAR(200) NULL, cp VARCHAR(6) NULL, ville VARCAR(60) NULL);"
If DatabaseUpdate(#MaBase, Requete$)
Debug "Table créée"
AddGadgetItem(#Liste, -1, "~ Liste des contacts ~" )
Alert("Nouveau projet créé",#Green)
Else
Alert("Impossible de mettre à jour la base")
EndIf
Else
Alert("Impossible de créer la base")
EndIf
EndProcedure
Procedure LectureBDD(BaseID, NomColonne$="prenom", ordre=0)
; On Ouvre la Base
Select ordre
Case #ASCENDANT
TRIE$ = " ASC"
Case #DESCENDANT
TRIE$ = " DESC"
Default
TRIE$ = " ASC"
EndSelect
If OpenDatabase(#MaBase, Filename$, "", "")
Debug "Connecté à PureBasic.sqlite"
; Si la base existe déjà, on lit les infos des contacts
If DatabaseQuery(#MaBase, "SELECT * FROM contacts ORDER BY "+NomColonne$+TRIE$) ; Recupère tous les enregistrements de la table 'contacts' par ordre alphabétique de prénoms
AddGadgetItem(#Liste, -1, "~ Liste des contacts ~" )
While NextDatabaseRow(#MaBase)
;Debug GetDatabaseString(#MaBase, 0) + " " + GetDatabaseString(#MaBase, 1) + " " + GetDatabaseString(#MaBase, 2)+ " " + GetDatabaseString(#MaBase, 3)
AddClientList(GetDatabaseString(#MaBase, 0),GetDatabaseString(#MaBase, 1),GetDatabaseString(#MaBase, 2),GetDatabaseString(#MaBase, 3),GetDatabaseString(#MaBase, 4),GetDatabaseString(#MaBase, 5),GetDatabaseString(#MaBase, 6),GetDatabaseString(#MaBase, 7))
AddGadgetItem(#Liste, -1, info()\prenom + " " + Info()\nom )
Wend
Debug "Fermeture de la requête"
FinishDatabaseQuery(#MaBase)
Else
;"On va créer une base contacts avec 2 contacts"
; La requête suivante va créer une table "contacts" avec les colonnes nom, prenom et email. Le champ email ne pourra pas être dupliqué)
Requete$ = "CREATE TABLE contacts (id INTEGER PRIMARY KEY, prenom VARCHAR(50) Not NULL, nom VARCHAR(50) Not NULL, email VARCHAR(100) NULL UNIQUE, tel VARCHAR (10) Not NULL UNIQUE, adresse VARCHAR(200) NULL, cp VARCHAR(6) NULL, ville VARCAR(60) NULL);"
If DatabaseUpdate(#MaBase, Requete$)
Debug "Table créée"
Insere(#MaBase, "Cyrille", "LDV","arsworld@gmail.com", "0698894598", "17 rue Ronsard","11160","Rieux Minervois" )
Insere(#MaBase, "Bob", "Marley","rastafari@threelittlebirds.com","0606060606","Rue du roots","11000","Kingston")
Debug "données insérées, affichage dans le comboBox"
EndIf
; Maintenant que les infos sont insérées, on les lit et les affiche
If DatabaseQuery(#MaBase, "SELECT * FROM contacts ORDER BY "+NomColonne$+TRIE$) ; Recupère tous les enregistrements de la table 'contacts'
AddGadgetItem(#Liste, -1, "~ Liste des contacts ~" )
While NextDatabaseRow(#MaBase)
;Debug GetDatabaseString(#MaBase, 0) + " " + GetDatabaseString(#MaBase, 1) + " " + GetDatabaseString(#MaBase, 2)+ " " + GetDatabaseString(#MaBase, 3)
AddClientList(GetDatabaseString(#MaBase, 0),GetDatabaseString(#MaBase, 1),GetDatabaseString(#MaBase, 2),GetDatabaseString(#MaBase, 3),GetDatabaseString(#MaBase, 4),GetDatabaseString(#MaBase, 5),GetDatabaseString(#MaBase, 6),GetDatabaseString(#MaBase, 7))
AddGadgetItem(#Liste, -1, info()\prenom + " " + Info()\nom )
Wend
Debug "Fermeture de la requête"
FinishDatabaseQuery(#MaBase)
EndIf
EndIf
EndIf
EndProcedure
;**********************************
; GESTION DE PROJET
;**********************************
Procedure MyMenu()
If CreateMenu(#Menu, WindowID(#WIN))
MenuTitle("Projet")
MenuItem(#M_Creer, "Nouveau" +Chr(9)+"Ctrl+N")
MenuItem(#M_Ouvrir, "Ouvrir" +Chr(9)+"Ctrl+O")
MenuItem(#M_Fermer, "Fermer" +Chr(9)+"Ctrl+F")
EndIf
EndProcedure
Procedure.s OpenProject()
Filename$ = OpenFileRequester("Selectionner un projet",GetCurrentDirectory(),"Base Sqlite (*.sqlite)|*.sqlite",0)
If Filename$
;Création du fichier qui contiendra la base.
If OpenFile(#Fichier, Filename$,#PB_File_SharedWrite)
Debug "Fichier de base de données ouvert"
CloseFile(#Fichier)
ProcedureReturn Filename$
Else
Alert("Impossible d'ouvrir le fichier")
EndIf
Else
Alert("Annulé par l'utilisateur")
EndIf
EndProcedure
Procedure.s NewProject()
Filename$ = InputRequester("Nouveau projet","Choisissez un nom pour le nouveau projet","")
If Filename$ > ""
Filename$+".sqlite"
If OpenFile(#Fichier, Filename$,#PB_File_SharedWrite)
Debug "Fichier de base de données créé"
CloseFile(#Fichier)
ProcedureReturn Filename$
Else
Alert("Impossible d'ouvrir le fichier")
EndIf
Else
Alert("Annulé par l'utilisateur")
EndIf
EndProcedure
;**********************************
; PROCEDURES ESTETHIQUES
;**********************************
Procedure Alert(message$, color=#Red)
SetGadgetColor(#Info,#PB_Gadget_FrontColor,color)
SetGadgetText(#info,message$)
EndProcedure
Procedure DisableStrings(etat=1)
DisableGadget(#PRENOM,etat)
DisableGadget(#NOM,etat)
DisableGadget(#EMAIL,etat)
DisableGadget(#TEL,etat)
DisableGadget(#ADRESSE,etat)
DisableGadget(#CP,etat)
DisableGadget(#VILLE,etat)
EndProcedure
Procedure Placeholder(Gadget,mot$)
If EventType() = #PB_EventType_Focus And GetGadgetText(Gadget) = mot$
SetGadgetText(Gadget,"")
SetGadgetColor(Gadget,#PB_Gadget_FrontColor,$0)
ElseIf EventType() = #PB_EventType_LostFocus And GetGadgetText(Gadget) = ""
SetGadgetColor(Gadget,#PB_Gadget_FrontColor,$AAAAAA)
SetGadgetText(Gadget, mot$)
EndIf
EndProcedure
Procedure ChangeStringsColor(color=$0)
SetGadgetColor(#PRENOM ,#PB_Gadget_FrontColor,color)
SetGadgetColor(#nom ,#PB_Gadget_FrontColor,color)
SetGadgetColor(#email ,#PB_Gadget_FrontColor,color)
SetGadgetColor(#tel ,#PB_Gadget_FrontColor,color)
SetGadgetColor(#ADRESSE ,#PB_Gadget_FrontColor,color)
SetGadgetColor(#cp ,#PB_Gadget_FrontColor,color)
SetGadgetColor(#ville ,#PB_Gadget_FrontColor,color)
EndProcedure
; =========== PROGRAMME ==========
If OpenWindow(#WIN, 0, 0, 470, 200, "SQLIte Demo", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
MyMenu()
ComboBoxGadget(#Liste, 10, 10, WindowWidth(#win)-20, 21, #PB_ComboBox_Editable)
DisableGadget(#Liste,1)
ButtonGadget(#BT_AJOUTER,2,37,140,25,"Ajouter un contact")
DisableGadget(#BT_AJOUTER,1)
StringGadget(#PRENOM, 143, 39, 92,20,"Prénom",#SS_CENTER)
SetGadgetAttribute(#PRENOM, #PB_String_MaximumLength,50)
SetGadgetColor(#Prenom,#PB_Gadget_FrontColor,$AAAAAA)
StringGadget(#NOM, 236, 39, 94,20,"Nom",#SS_CENTER)
SetGadgetAttribute(#NOM, #PB_String_MaximumLength,50)
SetGadgetColor(#NOM,#PB_Gadget_FrontColor,$AAAAAA)
StringGadget(#TEL, 333, 39, 130,20,"Tel",#SS_CENTER|#PB_String_Numeric)
SetGadgetAttribute(#TEL, #PB_String_MaximumLength,10)
SetGadgetColor(#TEL,#PB_Gadget_FrontColor,$AAAAAA)
StringGadget(#ADRESSE, 2, 62, 200,20,"Adresse",#SS_CENTER)
SetGadgetAttribute(#ADRESSE, #PB_String_MaximumLength,100)
SetGadgetColor(#ADRESSE,#PB_Gadget_FrontColor,$AAAAAA)
StringGadget(#CP, 203, 62, 128,20,"Code Postal",#PB_String_Numeric|#SS_CENTER)
SetGadgetAttribute(#CP, #PB_String_MaximumLength,5)
SetGadgetColor(#CP,#PB_Gadget_FrontColor,$AAAAAA)
StringGadget(#VILLE, 333, 62, 130,20,"Ville",#SS_CENTER)
SetGadgetAttribute(#VILLE, #PB_String_MaximumLength,100)
SetGadgetColor(#VILLE,#PB_Gadget_FrontColor,$AAAAAA)
StringGadget(#EMAIL, 2, 85, WindowWidth(#win)-4,20,"E-mail",#SS_CENTER)
SetGadgetAttribute(#EMAIL, #PB_String_MaximumLength,100)
SetGadgetColor(#EMAIL,#PB_Gadget_FrontColor,$AAAAAA)
DisableStrings()
ButtonGadget(#BT_MODIFIER,2,128,WindowWidth(#win)/2 -4,28,"Modifier le contact")
DisableGadget(#BT_MODIFIER,1)
ButtonGadget(#BT_SUPPR,WindowWidth(#win)/2 +2,128,WindowWidth(#win)/2-4,28,"Supprimer le contact")
DisableGadget(#BT_SUPPR,1)
ButtonGadget(#BT_ANNULER_MODIF,WindowWidth(#win)/2 +2,128,WindowWidth(#win)/2-4,28,"Annuler")
HideGadget(#BT_ANNULER_MODIF,1)
ButtonGadget(#BT_ANNULER_AJOUT,WindowWidth(#win)/2 +2,128,WindowWidth(#win)/2-4,28,"Annuler")
HideGadget(#BT_ANNULER_AJOUT,1)
StringGadget(#info,2,WindowHeight(#win)-42,WindowWidth(#win)-4,20,"Bienvenue",#PB_String3D_ReadOnly)
SetGadgetColor(#Info,#PB_Gadget_BackColor,$0) : SetGadgetColor(#Info,#PB_Gadget_FrontColor,#Green)
Repeat
Event = WaitWindowEvent()
Select Event
;- ** MENU
Case #PB_Event_Menu
Select EventMenu()
Case #M_Ouvrir
If OpenProject()
If IsDatabase(#mabase)
CloseDatabase(#MaBase)
ClearGadgetItems(#LISTE)
ClearList(info())
EndIf
LectureBDD(#MaBase)
DisableGadget(#BT_AJOUTER,0)
DisableGadget(#Liste,0)
SetGadgetState(#Liste, 0)
SetWindowTitle(#win,Filename$)
EndIf
Case #M_Creer
If NewProject() > ""
If IsDatabase(#mabase)
CloseDatabase(#MaBase)
ClearGadgetItems(#LISTE)
ClearList(info())
EndIf
CreateBase()
SetWindowTitle(#win,Filename$)
EndIf
EndSelect
Case #PB_Event_Gadget
Select EventGadget()
Case #PRENOM
Placeholder(#PRENOM,"Prénom")
Case #NOM
Placeholder(#NOM,"Nom")
Case #EMAIL
Placeholder(#EMAIL,"E-mail")
Case #TEL
Placeholder(#TEL,"Tel")
Case #ADRESSE
Placeholder(#ADRESSE,"Adresse")
Case #CP
Placeholder(#CP,"Code Postal")
Case #VILLE
Placeholder(#VILLE,"Ville")
Case #BT_ANNULER_AJOUT
Ajouter = #FERMER
DisableStrings(1)
SetGadgetText(#BT_AJOUTER, "Ajouter un contact")
SetGadgetText(#PRENOM,"Prénom")
SetGadgetText(#NOM,"Nom")
SetGadgetText(#EMAIL,"E-mail")
SetGadgetText(#TEL,"Tel")
SetGadgetText(#ADRESSE,"Adresse")
SetGadgetText(#CP,"Code Postal")
SetGadgetText(#VILLE,"Ville")
HideGadget(#BT_ANNULER_AJOUT,1)
HideGadget(#BT_SUPPR,0)
DisableGadget(#LISTE,0)
DisableGadget(#BT_SUPPR,1)
DisableGadget(#BT_MODIFIER,1)
HideGadget(#BT_MODIFIER,0)
ResizeGadget(#BT_AJOUTER,2,37,140,25)
Alert("Ajout annulé par l'utilisateur")
ResizeGadget(#PRENOM, 143, 39, 92,20)
ChangeStringsColor($AAAAAA)
Case #BT_AJOUTER ;- ** AJOUTER
If Ajouter = #FERMER
SetGadgetState(#Liste,0)
ChangeStringsColor()
SetGadgetText(#PRENOM,"Prénom")
SetGadgetText(#NOM,"Nom")
SetGadgetText(#EMAIL,"E-mail")
SetGadgetText(#TEL,"Tel")
SetGadgetText(#ADRESSE,"Adresse")
SetGadgetText(#CP,"Code Postal")
SetGadgetText(#VILLE,"Ville")
DisableGadget(#LISTE,1)
DisableStrings(0)
Ajouter = #OUVERT
SetGadgetText(#BT_AJOUTER, "Valider")
HideGadget(#BT_ANNULER_AJOUT,0)
HideGadget(#BT_MODIFIER,1)
HideGadget(#BT_SUPPR,1)
ResizeGadget(#BT_AJOUTER,2,128,WindowWidth(#win)/2-4,28)
ResizeGadget(#PRENOM, 2, 39, 141+92,20)
Else
PRENOM$ = GetGadgetText(#PRENOM)
NOM$ = GetGadgetText(#NOM)
EMAIL$ = GetGadgetText(#EMAIL)
TEL$ = GetGadgetText(#TEL)
ADRESSE$= GetGadgetText(#ADRESSE)
CP$ = GetGadgetText(#CP)
VILLE$ = GetGadgetText(#VILLE)
; 1ER TEST
If PRENOM$ = "" Or PRENOM$ = "prenom" Or NOM$ = "" Or NOM$ = "Nom" Or EMAIL$ = "" Or EMAIL$ ="E-mail" Or TEL$ = "" Or TEL$ ="Tel" Or ADRESSE$ = "" Or ADRESSE$ ="Adresse" Or CP$="" Or CP$ = "Code Postal" Or VILLE$ = "" Or VILLE$ ="Ville"
Debug "test 1"
Alert("Vous devez remplir tous les champs")
Else
; DEUXIEME TESTE
Debug "test 2"
If FindString(PRENOM$," ") Or FindString(NOM$," ") Or FindString(EMAIL$," ")
alert("Les Espaces et caractères spéciaux sont interdits")
Else
; TEST EMAIL
Debug "test 3"
If FindString(EMAIL$,"@") <> 0 And FindString(EMAIL$,".") <> 0 ; Format Email OK (test simpliste)
Insere(#MaBase, PRENOM$, NOM$, EMAIL$, TEL$, ADRESSE$, CP$, VILLE$)
ClearGadgetItems(#LISTE)
ClearList(info())
LectureBDD(#MaBase)
SetGadgetState(#liste,0)
SetGadgetText(#PRENOM,"Prénom")
SetGadgetText(#NOM,"Nom")
SetGadgetText(#EMAIL,"E-mail")
SetGadgetText(#TEL,"Tel")
SetGadgetText(#ADRESSE,"Adresse")
SetGadgetText(#CP,"Code Postal")
SetGadgetText(#VILLE,"Ville")
DisableGadget(#LISTE,0)
DisableStrings(1)
SetGadgetText(#BT_AJOUTER, "Ajouter un contact")
HideGadget(#BT_ANNULER_AJOUT,1)
HideGadget(#BT_SUPPR,0)
HideGadget(#BT_MODIFIER,0)
Ajouter = #FERMER
ResizeGadget(#BT_AJOUTER,2,37,140,25)
ResizeGadget(#PRENOM, 143, 39, 92,20)
ChangeStringsColor($AAAAAA)
Else
Alert("L'e-mail ne semble pas correct ")
EndIf
EndIf
EndIf
EndIf
Case #BT_SUPPR ;- ** SUPPRIMER
If Delete(#Mabase,INFO()\ID)
ClearGadgetItems(#LISTE)
ClearList(info())
LectureBDD(#MaBase)
SetGadgetState(#liste,0)
SetGadgetText(#PRENOM,"Prénom")
SetGadgetText(#NOM,"Nom")
SetGadgetText(#EMAIL,"E-mail")
SetGadgetText(#TEL,"Tel")
SetGadgetText(#ADRESSE,"Adresse")
SetGadgetText(#CP,"Code Postal")
SetGadgetText(#VILLE,"Ville")
DisableGadget(#BT_SUPPR,1)
Else
Alert(match$ ,#Green)
EndIf
Case #BT_MODIFIER ;- ** MODIFIER
If modifier = #ECRITURE
DisableGadget(#BT_SUPPR,1)
DisableGadget(#BT_AJOUTER,1)
SetGadgetText(#PRENOM,info()\prenom)
SetGadgetText(#NOM,info()\nom)
SetGadgetText(#EMAIL,info()\email)
SetGadgetText(#TEL,info()\tel)
SetGadgetText(#ADRESSE,info()\adresse)
SetGadgetText(#CP,info()\cp)
SetGadgetText(#VILLE,info()\ville)
DisableStrings(0)
ChangeStringsColor()
HideGadget(#BT_ANNULER_MODIF,0)
HideGadget(#BT_SUPPR,1)
SetGadgetText(#BT_MODIFIER,"Valider")
modifier = #VALIDATION
ElseIf modifier = #VALIDATION
;- UPDATE DE CONTACT
PRENOM$ = GetGadgetText(#PRENOM)
NOM$ = GetGadgetText(#NOM)
EMAIL$ = GetGadgetText(#EMAIL)
TEL$ = GetGadgetText(#TEL)
ADRESSE$= GetGadgetText(#ADRESSE)
CP$ = GetGadgetText(#CP)
VILLE$ = GetGadgetText(#VILLE)
; 1ER TEST
If PRENOM$ = "" Or PRENOM$ = "prenom" Or NOM$ = "" Or NOM$ = "Nom" Or EMAIL$ = "" Or EMAIL$ ="E-mail" Or TEL$ = "" Or TEL$ ="Tel" Or ADRESSE$ = "" Or ADRESSE$ ="Adresse" Or CP$="" Or CP$ = "Code Postal" Or VILLE$ = "" Or VILLE$ ="Ville"
Debug "test 1"
Alert("Vous devez remplir tous les champs")
Else
; DEUXIEME TESTE
Debug "test 2"
If FindString(PRENOM$," ") Or FindString(NOM$," ") Or FindString(EMAIL$," ")
alert("Les Espaces et caractères spéciaux sont interdits")
Else
; TEST EMAIL
Debug "test 3"
If FindString(EMAIL$,"@") <> 0 And FindString(EMAIL$,".") <> 0 ; Format Email OK (test simpliste)
Update(#MaBase, PRENOM$, NOM$, EMAIL$, TEL$, ADRESSE$, CP$, VILLE$)
HideGadget(#BT_SUPPR,0)
HideGadget(#BT_ANNULER_MODIF,1)
DisableGadget(#BT_SUPPR,0)
SetGadgetText(#BT_MODIFIER,"Modifier le contact")
DisableGadget(#BT_AJOUTER,0)
DisableGadget(#BT_MODIFIER,0)
DisableStrings(1)
modifier = #ECRITURE
Else
Alert("L'e-mail ne semble pas correcte ")
EndIf
EndIf
EndIf
EndIf
Case #BT_ANNULER_MODIF ;- ANNULE MODIF
N = GetGadgetState(#LISTE)
SelectElement(info(), N-1)
ClearGadgetItems(#LISTE)
ClearList(info())
LectureBDD(#MaBase)
SetGadgetState(#liste,N)
HideGadget(#BT_SUPPR,0)
HideGadget(#BT_ANNULER_MODIF,1)
DisableGadget(#BT_SUPPR,0)
SetGadgetText(#BT_MODIFIER,"Modifier le contact")
DisableGadget(#BT_AJOUTER,0)
; DisableGadget(#BT_MODIFIER,0)
DisableStrings(1)
modifier = #ECRITURE
;- LISTE
Case #Liste
N = GetGadgetState(#LISTE)
If N > 0
DisableGadget(#BT_MODIFIER,0)
DisableGadget(#BT_SUPPR,0)
SelectElement(info(), N-1)
Debug "Lancement de la recherche pour ID : " + INFO()\ID
If OldN <> N
SetGadgetText(#Info, AfficheInfo(#MaBase, INFO()\ID) )
OldN = N
EndIf
Else
OldN = N
DisableGadget(#BT_MODIFIER,1)
DisableGadget(#BT_SUPPR,1)
alert("Choisissez un contact dans la liste",#Green)
SetGadgetText(#PRENOM,"Prénom")
SetGadgetText(#NOM,"Nom")
SetGadgetText(#EMAIL,"E-mail")
SetGadgetText(#TEL,"Tel")
SetGadgetText(#ADRESSE,"Adresse")
SetGadgetText(#CP,"Code Postal")
SetGadgetText(#VILLE,"Ville")
EndIf
EndSelect
EndSelect
Until event = #PB_Event_CloseWindow
Else
End
EndIf
If IsDatabase(#mabase)
CloseDatabase(#MaBase)
EndIf