Обсуждение: [GENERAL] Where is the error?

Поиск
Список
Период
Сортировка

[GENERAL] Where is the error?

От
Igor Korot
Дата:
Hi, ALL,
I am trying to execute following piece of code:

[code]
    std::string query1 = "DECLARE alltables CURSOR SELECT
table_schema, table_name FROM information_schema.tables WHERE
table_type = 'BASE TABLE' OR table_type = 'VIEW' OR table_type =
'LOCAL TEMPORARY';";
    res = PQexec( m_db, query1.c_str() );
    if( PQresultStatus( res ) != PGRES_COMMAND_OK )
    {
        std::wstring err = m_pimpl->m_myconv.from_bytes(
PQerrorMessage( m_db ) );
        errorMsg.push_back( err );
        PQclear( res );
        return 1;
    }
[/code]

However running it gives an error in the query:

[quote]
RROR:  syntax error at or near "SELECT"
LINE 1: DECLARE alltables CURSOR SELECT table_schema, table_name FRO...

                             ^
[/quote]

The hat symbol is pointing to the "n" in the "table_name".

However trying to execute this query (without DECLARE part) inside
psql does not return any errors and runs fine.

What am I missing?

The code is written in C++ and compiled in C++11 mode with gcc 5 on
Gentoo Linux.

Thank you.


Re: [GENERAL] Where is the error?

От
Christoph Moench-Tegeder
Дата:
## Igor Korot (ikorot01@gmail.com):

>     std::string query1 = "DECLARE alltables CURSOR SELECT
> table_schema, table_name FROM information_schema.tables WHERE
> table_type = 'BASE TABLE' OR table_type = 'VIEW' OR table_type =
> 'LOCAL TEMPORARY';";

> [quote]
> RROR:  syntax error at or near "SELECT"
> LINE 1: DECLARE alltables CURSOR SELECT table_schema, table_name FRO...

> What am I missing?

A "FOR" in front of the "SELECT":
https://www.postgresql.org/docs/current/static/sql-declare.html

Note you can use cursors via psql, too - only they have to be in a
transaction block (but postgres will tell you, once you got the
syntax right).

Regards,
Christoph

--
Spare Space


Re: [GENERAL] Where is the error?

От
Igor Korot
Дата:
Hi, Christoph,



On May 6, 2017 2:24 PM, "Christoph Moench-Tegeder" <cmt@burggraben.net> wrote:
## Igor Korot (ikorot01@gmail.com):

>     std::string query1 = "DECLARE alltables CURSOR SELECT
> table_schema, table_name FROM information_schema.tables WHERE
> table_type = 'BASE TABLE' OR table_type = 'VIEW' OR table_type =
> 'LOCAL TEMPORARY';";

> [quote]
> RROR:  syntax error at or near "SELECT"
> LINE 1: DECLARE alltables CURSOR SELECT table_schema, table_name FRO...

> What am I missing?

A "FOR" in front of the "SELECT":
https://www.postgresql.org/docs/current/static/sql-declare.html

Another question - do I have to do "DECLARE CURSOR..." or I can just write a normal SELECT query?
For this I followed and example on the libpg page...

Thank you.



Note you can use cursors via psql, too - only they have to be in a
transaction block (but postgres will tell you, once you got the
syntax right).

Regards,
Christoph

--
Spare Space


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Re: [GENERAL] Where is the error?

От
Christoph Moench-Tegeder
Дата:
## Igor Korot (ikorot01@gmail.com):

> Another question - do I have to do "DECLARE CURSOR..." or I can just write
> a normal SELECT query?
> For this I followed and example on the libpg page...

Yes. In fact, "libpq Example Program 3" on
https://www.postgresql.org/docs/current/static/libpq-example.html
does exactly that.

Regards,
Christoph

--
Spare Space