Some other thoughts for you, in no particular order:

* I encourage you to always specify your field names in a SQL query, as it
is both more readable and it will future-proof you against changes in the
schema.  So:

    Use this:  INSERT INTO questions (description, qtext, author) VALUES
(...data...);
    Not this:   INSERT INTO questions VALUES (...data...);

* With Racket, look at query-rows and related functions.  They can be a lot
easier than query.

* If you happen to be using PostgreSQL there is a very convenient extension
to the SQL standard: RETURNING.
https://www.postgresql.org/docs/current/static/dml-returning.html

    (query-value db "INSERT INTO questions (descriptions, qtext, author)
VALUES ($1, $2, $3) RETURNING id" descript question-text author)  ; This
will return the id of the row that it just inserted

* <shameless="self promotion">You might want to take a look at the
query-rows-as-dicts family of functions in handy/db  </shameless>

;  This query is being sent against a PG database
(query-rows-as-dicts
   '(first-name last-name titles)
   db
  "SELECT first_name, last_name, ARRAY_AGG(title) FROM users u LEFT JOIN
titles t on u.id = t.user_id GROUP BY first_name, last_name"
  #:transform-data (make-transform-data-func sql-null? "unknown" pg-array?
pg-array->list string? string-trim)
  #:transform-dict (lambda (d) (hash-set d 'titles (map string-downcase
(hash-ref d 'titles)))))

This returns a list of hashes that might look like so:
(list
  (hash 'first-name "bob" 'last-name "smith" 'titles '("ceo" "founder")) ;
in the database it's CEO, but we lowercased it
  (hash 'first-name "alice" 'last-name "unknown" 'titles '("team leader"))
; in the database her last_name field is NULL
  (hash 'first-name "joe" 'last-name "smith" 'titles ()) ; in the database
his last name is "  smith" with leading whitespace
)

The regular db module would have returned the 'title' field as a pg-array
struct which you would then have had to convert via pg-array->list, but
here the query does it for you.


On Thu, Sep 27, 2018 at 4:14 PM George Neuner <gneun...@comcast.net> wrote:

>
>
> On 9/27/2018 4:07 PM, Vincent St-Amour wrote:
> > Olá,
> >
> > Se eu entendi sua pergunta, a função `dict-ref` é o que você quer:
> >
> >      (dict-ref (query ..) 'insert-id)
> >
> > Vincent
>
> Yes, that will work on the alist.  But first you have to get the alist
> out of the simple-result struct.
>
> George
>
> --
> 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.
>

-- 
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