agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: john <johnf@jfcomputer.com>
To: pgsql-sql@lists.postgresql.org
Subject: Re: What is the right syntax for retrieving the last_insert_id() in Postgresql ?
Date: Tue, 3 Mar 2020 08:04:44 -0800
Message-ID: <546aa670-4ff5-01dd-7c37-0e69fdfe6c85@jfcomputer.com> (raw)
In-Reply-To: <365718595.2569244.1583250608503@mail.yahoo.com>
References: <1063179739.1806448.1583057868061.ref@mail.yahoo.com>
	<1063179739.1806448.1583057868061@mail.yahoo.com>
	<CAFj8pRAt1nCQTdkXHrO3apcckp86xKEY7xgvrw-qUuhP46HY0Q@mail.gmail.com>
	<365718595.2569244.1583250608503@mail.yahoo.com>

currval('id_seq') where id_seq is the sequence you are using for the key 
(t_id). That assumes you are using a sequence.
Johnf

On 3/3/20 7:50 AM, Karen Goh wrote:
> Hi Pavel,
>
> Using this as reference and your link :
>
> https://dba.stackexchange.com/questions/3281/how-do-i-use-currval-in-postgresql-to-get-the-last-inse...
>
> I tried :
>
> select lastval('t_id') from table_tutor;
>
> but it is not working.
>
> Is there any tutorial out there that teaches the exact syntax ?
>
> Thanks & regards,
> Karen
> On Sunday, March 1, 2020, 06:27:00 PM GMT+8, Pavel Stehule 
> <pavel.stehule@gmail.com> wrote:
>
>
>
>
> ne 1. 3. 2020 v 11:18 odesílatel Karen Goh <karenworld@yahoo.com 
> <mailto:karenworld@yahoo.com>> napsal:
>
>     Hi,
>
>     I hope I am posting on the right forum.  I googled but I can't
>     find any solution pertaining to my problem.
>
>     Could someone know what is the syntext for last_insert_id() in
>     Postgresql for me to insert into my sql execute query?
>
>
> Postgres has not last_insert_id function. Maybe you think "lastval" 
> function
>
> https://www.postgresql.org/docs/current/functions-sequence.html
>
> Regards
>
> Pavel
>
>
>     public int getTutorById() {
>                     openConnection();
>                     int tutor_id = 0;
>                     try {
>                             Statement stmt3 =
>     connection.createStatement();
>                             ResultSet rs = stmt3.executeQuery("SELECT
>     last_insert_id() from xtutor");
>                             {
>                                     while (rs.next()) {
>                                             tutor_id = rs.getInt(1);
>                                     }
>                             }
>                     } catch (SQLException e) {
>                             e.printStackTrace();
>                     }
>                     return tutor_id;
>             }
>
>     org.postgresql.util.PSQLException: ERROR: function
>     last_insert_id() does not exist
>       Hint: No function matches the given name and argument types. You
>     might need to add explicit type casts.
>       Position: 8
>             at
>     org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
>             at
>     org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)
>             at
>     org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)
>             at
>     org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
>             at
>     org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
>             at
>     org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:307)
>             at
>     org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:293)
>             at
>     org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:270)
>             at
>     org.postgresql.jdbc.PgStatement.executeQuery(PgStatement.java:224)
>             at daoMySql.tutorDAOImpl.getTutorById(tutorDAOImpl.java:202)
>             at daoMySql.tutorDAOImpl.getAlltutors(tutorDAOImpl.java:161)
>             at business.manager.getAlltutors(manager.java:99)
>
>     The generated id is successfully generated by JDBC.
>
>     The errors when I tested in out using PGAdmin4 is
>
>     ERROR:  function last_insert_id() does not exist
>     LINE 1: SELECT last_insert_id() from xtutor;
>                    ^
>     HINT:  No function matches the given name and argument types. You
>     might need to add explicit type casts.
>     SQL state: 42883
>     Character: 8
>
>     Postgresql 11, Windows 11
>
>     Thanks.
>
>
>

view thread (7+ messages)  latest in thread

Message-ID: <546aa670-4ff5-01dd-7c37-0e69fdfe6c85@jfcomputer.com>
Permalink:  ../546aa670-4ff5-01dd-7c37-0e69fdfe6c85@jfcomputer.com/
Also on:    postgresql.org/message-id/546aa670-4ff5-01dd-7c37-0e69fdfe6c85@jfcomputer.com

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: johnf@jfcomputer.com, pgsql-sql@lists.postgresql.org
  Subject: Re: What is the right syntax for retrieving the last_insert_id() in Postgresql ?
  In-Reply-To: <546aa670-4ff5-01dd-7c37-0e69fdfe6c85@jfcomputer.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox