PostgreSQL

Скачать в pdf «PostgreSQL»


The sqlprint command is used with the EXEC SQL WHENEVER statement to turn on error handling throughout the program:


EXEC SQL WHENEVER sqlerror sqlprint;


and


EXEC SQL WHENEVER not found sqlprint;


Note: This is not an exhaustive example of usage for the EXEC SQL WHENEVER statement. Further examples of usage may be found in SQL manuals (e.g., The LAN TIMES Guide to SQL by Groff and Weinberg).


Connecting to the Database Server


One connects to a database using the following:


EXEC SQL CONNECT dbnarn;


where the database name is not quoted. Prior to version 2.1.0, the database name was required to be inside single quotes.


Specifying a server and port name in the connect statement is also possible. The syntax is: dbname [@serrer ][:port ]


or


<tcp|unix> :postgresql://serrer [:port ][fdbname ][?options ]


Queries


In general, SQL queries acceptable to other applications such as psql can be embedded into your C code. Here are some examples of how to do that.


Create Table:


EXEC SQL CREATE TABLE foo (number int4, ascii char(16)};


EXEC SQL CREATE UNIQUE index numl on foo(number);


EXEC SQL COMMIT;


Insert:


EXEC SQL INSERT INTO foo (number, ascii) VALUES (9999, ‘doodad’);


EXEC SQL COMMIT;


Delete:


EXEC SQL DELETE FROM foo WHERE number = 9999;


EXEC SQL COMMIT;


Singleton Select:


EXEC SQL SELECT foo INTO :FooBar FROM tablel WHERE ascii = ‘doodad’;


Select using Cursors:

Скачать в pdf «PostgreSQL»