wrong using insert into ...values(collection<?>)

21 views
Skip to first unread message

ysji...@gmail.com

unread,
Feb 23, 2017, 7:02:50 AM2/23/17
to jOOQ User Group
HI ,I using JOOQ to build sql only, the code can see below:

DSLContext create= DSL.using(SQLDialect.POSTGRES_9_5);
List<Field<?>> columns=new ArrayList<>();
List<Field<?>> values=new ArrayList<>();
columns.add(DSL.field("id"));
columns.add(DSL.field("name"));
columns.add(DSL.field("age"));
values.add(DSL.field("003"));
values.add(DSL.field("bob"));
values.add(DSL.field("24"));
String sql=create.insertInto(DSL.table("task")).columns(columns).values(values).getSQL();


The sql is: insert into task (id, name, age) values (003, bob, 24)
But it's wrong! it should be: insert into task (id, name, age) values ('003', 'bob', '24')

Where the problem is? Many thanks for your answer~!

Lukas Eder

unread,
Feb 24, 2017, 5:07:03 AM2/24/17
to jooq...@googlegroups.com
Hello,

This is the expected behaviour of jOOQ.

Your mistake is that you're using the plain SQL API to create what you believe to be bind variables.

DSL.field() is for plain SQL:

DSL.val() is for bind variables:

DSL.inline() is for inline values (literals):
values.add(DSL.inline("003"));
values.add(DSL.inline("bob")); values.add(DSL.inline("24"));
I hope this helps,
Lukas

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

姜岳松

unread,
Feb 24, 2017, 9:46:01 PM2/24/17
to jOOQ User Group
Yes! it's what I need! thanks very much. I am just new for JOOQ and the document maybe a little confused for me 

在 2017年2月24日星期五 UTC+8下午6:07:03,Lukas Eder写道:
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages