Produit : 4D for OCI 2004 · Plateforme : Mac & Win ♪
Voici un code exemple qui récupère les données dans une colonne Oracle de type BLOB avec les commandes du plugin 4D for OCI 2004 :
` ----------------------------------------------------
` User name (OS): Stirtiat
` Date and time: 27/06/07, 18:42:22
` ----------------------------------------------------
` Method: OCI_LIRE_BLOB
` ----------------------------------------------------
`paramètres de connexion à modifier
$user:="xxx"
$password:="xxx"
$server:="xxx"
`login complexe
status:=OCIEnvCreate (envhp;OCI_DEFAULT )
status:=OCIHandleAlloc (envhp;errhp;OCI_HTYPE_ERROR )
status:=OCIHandleAlloc (envhp;svchp;OCI_HTYPE_SVCCTX )
status:=OCIHandleAlloc (envhp;authp;OCI_HTYPE_SESSION )
status:=OCIHandleAlloc (envhp;srvhp;OCI_HTYPE_SERVER )
status:=OCIServerAttach (srvhp;errhp;$server)
status:=OCIAttrSetVal (svchp;srvhp;OCI_ATTR_SERVER ;errhp)
status:=OCIAttrSetText (authp;$user;OCI_ATTR_USERNAME ;errhp)
status:=OCIAttrSetText (authp;$password;OCI_ATTR_PASSWORD ;errhp)
status:=OCISessionBegin (svchp;errhp;authp;OCI_CRED_RDBMS ;OCI_DEFAULT )
status:=OCIAttrSetVal (svchp;authp;OCI_ATTR_SESSION ;errhp)
`requête SQL
$sql_statement:="SELECT t_blob FROM test_lob WHERE t_id = 1"
C_ENTIER LONG(bloblocator)
C_ENTIER LONG($define;$position;$sqlt)
C_POINTEUR($p_define)
C_ENTIER LONG(null_ind01;rlen01;rcode01)
bloblocator:=0
$define:=0
$position:=1
$p_define:=->bloblocator
$sqlt:=SQLT_BLOB
C_BLOB($blob)
`préparation requête
status:=OCIHandleAlloc (envhp;stmthp;OCI_HTYPE_STMT )
status:=OCIDescriptorAlloc (envhp;bloblocator;OCI_DTYPE_LOB )
status:=OCIStmtPrepare (stmthp;errhp;$sql_statement;OCI_DEFAULT )
status:=OCIDefineByPos (stmthp;$define;errhp;$position;$p_define;$sqlt;->null_ind01;->rlen01;->rcode01;OCI_DEFAULT )
status:=OCIStmtExecute (svchp;stmthp;errhp;1;0;0;0;OCI_DEFAULT )
status:=OCILobRead (svchp;errhp;bloblocator;1;$blob)
`libération
status:=OCIDescriptorFree (bloblocator)
status:=OCIHandleFree (stmthp)
status:=OCISessionEnd (svchp;errhp;authp)
status:=OCIServerDetach (srvhp;errhp)
status:=OCICleanUp
`récupération du blob dans un fichier
Si (Taille BLOB($blob)>0)
`si la colonne BLOB Oracle a été alimentée par 4D for Oracle,
`il faut retirer les 8 premiers octets du BLOB reçu
`SUPPRIMER DANS BLOB($blob;0;8)
`on nomme en dur le fichier mais on peut créer le nom par programmation
`et récupérer le type de fichier dans le blob pour l'extension
` --voir astuce "Tester des documents image par leur marqueur"(*)
$DocRef:=Creer document("image_1.jpg")
Si (OK=1)
FERMER DOCUMENT($DocRef)
BLOB VERS DOCUMENT(Document;$blob)
Fin de si
Sinon
ALERTE("Pas de contenu dans le blob !")
Fin de si
`on vide le blob
FIXER TAILLE BLOB($blob;0)Description de la table Oracle qui a servi à cet exemple :
CREATE TABLE TEST_LOB (
T_ID NUMBER(5,0),
T_NAME VARCHAR2(80),
T_BLOB BLOB
);Compléments d'information :
· Documentations Oracle et 4D for OCI
· 4D for OCI : Lire une colonne Oracle de type LONG RAW
· 4D for OCI : Écrire dans une colonne Oracle de type LONG RAW
· 4D ODBC Pro : Lire une colonne Oracle de type BLOB
· 4D ODBC Pro : Écrire dans une colonne Oracle de type BLOB
· Introduction aux OCI
· 4D et les autres SGBDR
(*) Tester des documents image par leur marqueur



