On 9/27/2018 3:24 PM, dev.vide...@gmail.com wrote:
Olá, sou iniciante no racket. Gostaria de capturar o valor *insert-id* que retorna em um uma struct chama simple-result <https://docs.racket-lang.org/db/query-api.html#%28def._%28%28lib._db%2Fbase..rkt%29._simple-result%29%29> da query <https://docs.racket-lang.org/db/query-api.html#%28def._%28%28lib._db%2Fbase..rkt%29._query%29%29> que executo. O retorno é o seguinte:

|(simple-result '((insert-id . 30) (affected-rows . 1))) |

Gostaria de obter o valor *30* por exemplo.
Código da execução da query:

|(define save_pergunta (lambda (tf_pergunta) (define result_save_pergunta (query conn "INSERT INTO perguntas VALUES (null, $pergunta)" tf_pergunta)) (print result_save_pergunta) (printf "\nPergunta Cadastrada!\n")))|

*simple-result* is a struct - you need to extract what you want from the association list in the info field.

The function to extract a field from a struct is [usually] the concatenation of the field name with the struct name: so the *info* field in *simple-result* would be  (*simple-result-info* <the-struct>).

You find an entry in an association list with *assoc* [or *assv* or *assq* depending on the type of the key].  The entry itself is a cons pair, so you need to extract the 2nd value in a pair with *cdr*.

So to get the identifier you need to do something like the following:

  (require db)
  (let (
        (result (query conn "insert into questions values (null,$question)" tf_question))
    (cdr (assoc 'insert-id (simple-result-info result))))

Note the above is not checking for errors.  You should always check that the result really is a simple-result before trying to extract the info because query can return other kinds of results (including no results ... query can throw an exception if it fails).

Hope this helps,

You received this message because you are subscribed to the Google Groups "Racket 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to