I’m not sure on the error you're getting (though, it did yell at me over the =
sign in your insert statement), but this worked in my irb session. DB is the
postgresql database I have already defined in my code.
> DB.run('create table test ( data json )’)
=> nil
> DB.run("insert into test (data) values ('{\"email\": \"[email protected]\",
> \"fullname\": \"john doe\"}')”)
=> nil
> DB.run('insert into test (data) values (\'{"email": "[email protected]",
> "fullname": "Jimmie Doe"}\')’)
=> nil
> DB['select data from test'].all
=> [{:data=>"{\"email\": \"[email protected]\", \"fullname\": \"john doe\"}"},
{:data=>"{\"email\": \"[email protected]\", \"fullname\": \"Jimmie Doe\"}”}]
Again, did get an error based on your sample:
> DB.run("insert into test (data) values ('{\"email\" => \"[email protected]\",
> \"fullname\": \"john doe\"}')")
Sequel::DatabaseError: PG::InvalidTextRepresentation: ERROR: invalid input
syntax for type json
LINE 1: insert into test (data) values ('{"email" => "[email protected]",...
^
DETAIL: Token "=" is invalid.
CONTEXT: JSON data, line 1: {"email" =…
And, yes, DB.run(“select …”) will return nil, since you’re actually after a
different syntax. I think there’s a couple ways to do what you’re after, if
you don’t like the syntax I tried above. A quick look at the cheat sheet might
be helpful.
-Joe
> On Jun 22, 2015, at 11:49 AM, Tiger Nassau <[email protected]> wrote:
>
> trying to make some REST type calls from sinatra using postgres jsonb. each
> of our tables is like : id: primary key, data: jsonb
>
> db.run ('create table .....') runs fine and creates table
>
> res = db.run("select data from signups;")
> post res
> ==> runs without error but no output
>
>
> db.run(insert into signups (data) values ('{"email"=>"[email protected]",
> "full_name": "john doe"}') )
> ==> does not run - the db.run wants quotes but so does the postgres values
> statement - tried to escape the quotes with \' but that didn't work either
>
> db[:posts].insert(:data=>Sequel.pg_jsonb(data))
> ==> works fine but is hard to call from function
>
> we'll have to run delete as well but that should be easy if we can get db.run
> (....) to work
>
> any suggestions are we missing something simple with db.run ?
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sequel-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected]
> <mailto:[email protected]>.
> To post to this group, send email to [email protected]
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/sequel-talk
> <http://groups.google.com/group/sequel-talk>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.