Jump to content
Fivewin Brasil

Salvar e Recuperar Array em String


Theotokos

Recommended Posts

Bom Dia!!!

Tenho uma array assim: { {Codigo, Qtde, Preco} } e preciso gravar ele em BD no campo String e depois recupera

Exemplo: { {00001, 2, 34.50}, {00005, 1, 50.40}, {001230, 3, 12.34} }

Preciso Salvar esta array em um campo STRING (Memo) e depois recuperar de forma que fique esta array...

Atualmente uso Hb_aTokens, mas ele recuperar como Vetor, assim { 00001, 2, 34.50, 00005, 1, 50.40, 001230, 3, 12.34 }

Link to comment
Share on other sites

Pega ai essa bem simplificada e eficaz.

A funcao u_Teste é um exemplo de uso

 

ArrayToString transforma o array em string (na verdade um json)

StringToArray lê o json salvo em arraytostring e devolve o array pronto

 

Function u_Teste()
Local aDados := {}
Local nI
 
For nI := 1 to 10
    aadd(aDados, nI)
Next
 
cSave_Array := ArrayToString(aDados)
 
aNew_Array := StringToArray(cSave_Array)
 
MsgStop(Sr_ShowVector(aNew_Array))
 
Return


 
Static Function ArrayToString(aDados)
Local hDados := Hash()
 
hDados["ARRAY"] := aDados
 
Return hb_jsonEncode(hDados,.t.)
 
Static Function StringToArray(cDados)
Local hDados := Hash()
Local aDados := {}
 
try
    hb_jsondecode(cDados, @hDados)
    aDados := hDados["ARRAY"]
catch
end
 
Return aDados
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...