Hi,
You get the generated key by calling the getKey() method after you have executed your query.
long key = sql2o.createQuery(...)
.addParameter(...)
.executeUpdate()
.getKey(Long.class);
By the way, you don't need the column mappings when inserting into your database. Column mappings are only used with select-queries.
May I ask which database you are connecting to?
Regards
Lars Aaberg
--
You received this message because you are subscribed to the Google Groups "sql2o" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sql2o+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
May I ask which database you are connecting to?
May I ask which database you are connecting to?PostgreSQL 9.2
--
INSERT INTO public.shmuel_test_tbl( id, string_col, time_col)
VALUES (DEFAULT,
now(),
now());
CREATE TABLE public.shmuel_test_tbl
(
id integer NOT NULL DEFAULT nextval('shmuel_test_id_seq'::regclass),
string_col character varying(50) ,
time_col timestamp without time zone,
CONSTRAINT shmuel_tbl_pk PRIMARY KEY (id),
CONSTRAINT string_col_uniq UNIQUE (string_col)
)
rg.postgresql.util.PSQLException: Cannot rollback when autoCommit is enabled.
at org.postgresql.jdbc.PgConnection.rollback(PgConnection.java:776)
at org.sql2o.Connection.rollback(Connection.java:115)
at org.sql2o.Connection.onException(Connection.java:56)
at org.sql2o.Query.executeUpdate(Query.java:526)
at com.github.ghost93.dal.postgres.PostgresDal.shmuelTest(PostgresDal.java:119)
at com.github.ghost93.api.Application.main(Application.java:28)
Exception in thread "main" org.sql2o.Sql2oException: Error in executeUpdate, No value specified for parameter 1.
at org.sql2o.Query.executeUpdate(Query.java:527)
at com.github.ghost93.dal.postgres.PostgresDal.shmuelTest(PostgresDal.java:119)
at com.github.ghost93.api.Application.main(Application.java:28)
Caused by: org.postgresql.util.PSQLException: No value specified for parameter 1.
at org.postgresql.core.v3.SimpleParameterList.checkAllParametersSet(SimpleParameterList.java:257)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:281)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:428)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:354)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:169)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:136)
at org.sql2o.Query.executeUpdate(Query.java:521)
... 2 more
ShmuelTestModel modelToInsert = new ShmuelTestModel();
con.createQuery(
"INSERT INTO public.shmuel_test_tbl(id, string_col, time_col) VALUES (DEFAULT,:str, :time)",true)
.bind(modelToInsert).executeUpdate();
Hi!
Looks like it might be the parameter mapping between the query and your model that's the problem.
What do your model class look like?
~Lars
For more options, visit https://groups.google.com/d/optout.
class ShmuelTestModel {
private String str = "Hey@"+System.currentTimeMillis();
private Date time = new Date(System.currentTimeMillis()-24*60*60*1000);
private int id;
}