2 single: PostgreSQL; はじめに
8 PostgreSQL データベースの用法を学びます。
11 pair: PostgreSQL; ライブラリの読み込み
16 この関数の使用前に postgresqllib.ring ライブラリを読み込みます。
20 load "postgresqllib.ring"
33 load "postgresqllib.ring"
35 conninfo = "user=postgres password=sa dbname = postgres"
37 exit_nicely = func conn {
42 conn = PQconnectdb(conninfo)
44 if (PQstatus(conn) != CONNECTION_OK)
45 fputs(stderr, "Connection to database failed: "+PQerrorMessage(conn))
46 call exit_nicely(conn)
49 res = PQexec(conn, "select * from pg_database")
50 if PQresultStatus(res) != PGRES_TUPLES_OK
51 fputs(stderr, "Select failed: " + PQerrorMessage(conn))
56 nFields = PQnfields(res)
63 for i = 1 to PQntuples(res)
65 see PQgetvalue(res, i-1, j-1) + " "
91 ************************************************************
92 postgres 10 6 English_United States.1252
93 English_United States.1252 f t -1 12937 549 1 1663
94 template1 10 6 English_United States.1252 English_United States.1252
95 t t -1 12937 549 1 1663 {=c/postgres,postgres=CTc/postgres}
96 template0 10 6 English_United States.1252 English_United States.1252
97 t f -1 12937 549 1 1663 {=c/postgres,postgres=CTc/postgres}
98 mahdb 10 6 English_United States.1252 English_United States.1252
99 f t -1 12937 549 1 1663
105 load "postgresqllib.ring"
107 conninfo = "user=postgres password=sa dbname = mahdb"
109 exit_nicely = func conn {
114 conn = PQconnectdb(conninfo)
116 if (PQstatus(conn) != CONNECTION_OK)
117 fputs(stderr, "Connection to database failed: "+PQerrorMessage(conn))
118 call exit_nicely(conn)
124 if PQresultStatus(res) != PGRES_TUPLES_OK
125 fputs(stderr, "Remove failed: " + PQerrorMessage(conn))
131 res = PQexec(conn, "CREATE DATABASE mahdb;")
132 if PQresultStatus(res) != PGRES_TUPLES_OK
133 fputs(stderr, "Create database failed: " + PQerrorMessage(conn))
139 CREATE TABLE COMPANY (
140 ID INT PRIMARY KEY NOT NULL,
146 if PQresultStatus(res) != PGRES_TUPLES_OK
147 fputs(stderr, "Create Table failed: " + PQerrorMessage(conn))
153 INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
154 VALUES (1, 'Mahmoud' , 31, 'Jeddah', 10.00 ),
155 (2, 'Ahmed' , 27, 'Jeddah', 20.00 ),
156 (3, 'Mohammed', 33, 'Egypt' , 30.00 ),
157 (4, 'Ibrahim' , 24, 'Egypt ', 40.00 );
159 if PQresultStatus(res) != PGRES_TUPLES_OK
160 fputs(stderr, "Insert Table failed: " + PQerrorMessage(conn))
166 select * from COMPANY
168 if PQresultStatus(res) != PGRES_TUPLES_OK
169 fputs(stderr, "Select failed: " + PQerrorMessage(conn))
171 call exit_nicely(conn)
175 nFields = PQnfields(res)
182 for i = 1 to PQntuples(res)
184 see PQgetvalue(res, i-1, j-1) + " "
202 ************************************************************
203 1 Mahmoud 31 Jeddah 10
205 3 Mohammed 31 Egypt 30
206 4 Ibrahim 24 Egypt 40
210 pair: PostgreSQL; RingPostgreSQL 定数
215 この定数は RingPostgreSQL ライブラリで定義済みです。
221 CONNECTION_AWAITING_RESPONSE
223 CONNECTION_SSL_STARTUP
245 PG_DIAG_MESSAGE_PRIMARY
246 PG_DIAG_MESSAGE_DETAIL
248 PG_DIAG_STATEMENT_POSITION
249 PG_DIAG_INTERNAL_POSITION
250 PG_DIAG_INTERNAL_QUERY
255 PG_DIAG_DATATYPE_NAME
256 PG_DIAG_CONSTRAINT_NAME
259 PG_DIAG_SOURCE_FUNCTION
263 pair: PostgreSQL; RingPostgreSQL 関数
268 この関数は RingPostgreSQL ライブラリで定義されています。
270 リファレンス : https://www.postgresql.org/docs/9.1/static/libpq.html
274 PGconn *PQconnectdbParams(const char **keywords,
275 const char **values,int expand_dbname);
276 PGconn *PQconnectdb(const char *conninfo)
277 PGconn *PQsetdbLogin(const char *pghost,const char *pgport,
278 const char *pgoptions,const char *pgtty,
279 const char *dbName,const char *login,const char *pwd)
280 PGconn *PQsetdb(char *pghost,char *pgport,char *pgoptions,
281 char *pgtty,char *dbName)
282 PGconn *PQconnectStartParams(const char **keywords,
283 const char **values,int expand_dbname)
284 PGconn *PQconnectStart(const char *conninfo)
285 PostgresPollingStatusType PQconnectPoll(PGconn *conn)
286 PQconninfoOption *PQconndefaults(void)
287 PQconninfoOption *PQconninfo(PGconn *conn)
288 PQconninfoOption *PQconninfoParse(const char *conninfo, char **errmsg)
289 void PQfinish(PGconn *conn)
290 void PQreset(PGconn *conn)
291 int PQresetStart(PGconn *conn)
292 PostgresPollingStatusType PQresetPoll(PGconn *conn)
293 PGPing PQpingParams(const char **keywords,const char **values,
295 PGPing PQping(const char *conninfo)
296 char *PQdb(const PGconn *conn)
297 char *PQuser(const PGconn *conn)
298 char *PQpass(const PGconn *conn)
299 char *PQhost(const PGconn *conn)
300 char *PQport(const PGconn *conn)
301 char *PQtty(const PGconn *conn)
302 char *PQoptions(const PGconn *conn)
303 ConnStatusType PQstatus(const PGconn *conn)
304 PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
305 const char *PQparameterStatus(const PGconn *conn, const char *paramName)
306 int PQprotocolVersion(const PGconn *conn)
307 int PQserverVersion(const PGconn *conn)
308 char *PQerrorMessage(const PGconn *conn)
309 int PQsocket(const PGconn *conn)
310 int PQbackendPID(const PGconn *conn)
311 int PQconnectionNeedsPassword(const PGconn *conn)
312 int PQconnectionUsedPassword(const PGconn *conn)
313 int PQsslInUse(const PGconn *conn)
314 const char *PQsslAttribute(const PGconn *conn, const char *attribute_name)
315 const char **PQsslAttributeNames(const PGconn *conn)
316 void *PQsslStruct(const PGconn *conn, const char *struct_name)
317 void *PQgetssl(const PGconn *conn)
318 PGresult *PQexec(PGconn *conn, const char *command);
319 PGresult *PQexecParams(PGconn *conn,const char *command,int nParams,
320 const Oid *paramTypes,const char **paramValues,
321 const int *paramLengths,const int *paramFormats,int resultFormat)
322 PGresult *PQprepare(PGconn *conn,const char *stmtName,
323 const char *query,int nParams,const Oid *paramTypes)
324 PGresult *PQexecPrepared(PGconn *conn,const char *stmtName,
325 int nParams,const char **paramValues,
326 const int *paramLengths,const int *paramFormats,int resultFormat)
327 PGresult *PQdescribePrepared(PGconn *conn, const char *stmtName)
328 PGresult *PQdescribePortal(PGconn *conn, const char *portalName)
329 ExecStatusType PQresultStatus(const PGresult *res)
330 char *PQresStatus(ExecStatusType status)
331 char *PQresultErrorMessage(const PGresult *res)
332 char *PQresultErrorField(const PGresult *res, int fieldcode)
333 void PQclear(PGresult *res)
334 int PQntuples(const PGresult *res)
335 int PQnfields(const PGresult *res)
336 char *PQfname(const PGresult *res,int column_number)
337 int PQfnumber(const PGresult *res,const char *column_name)
338 Oid PQftable(const PGresult *res,int column_number)
339 int PQftablecol(const PGresult *res,int column_number)
340 int PQfformat(const PGresult *res,int column_number)
341 Oid PQftype(const PGresult *res,int column_number)
342 int PQfmod(const PGresult *res,int column_number)
343 int PQfsize(const PGresult *res,int column_number)
344 int PQbinaryTuples(const PGresult *res)
345 char *PQgetvalue(const PGresult *res,int row_number,int column_number)
346 int PQgetisnull(const PGresult *res,int row_number,int column_number)
347 int PQgetlength(const PGresult *res,int row_number,int column_number)
348 int PQnparams(const PGresult *res)
349 Oid PQparamtype(const PGresult *res, int param_number)
350 void PQprint(FILE *fout,const PGresult *res,const PQprintOpt *po)
351 char *PQcmdStatus(PGresult *res)
352 char *PQcmdTuples(PGresult *res)
353 Oid PQoidValue(const PGresult *res)
354 char *PQoidStatus(const PGresult *res)
355 char *PQescapeLiteral(PGconn *conn, const char *str, size_t length)
356 char *PQescapeIdentifier(PGconn *conn, const char *str, size_t length)
357 size_t PQescapeStringConn(PGconn *conn,char *to,
358 const char *from, size_t length,int *error)
359 size_t PQescapeString(char *to, const char *from, size_t length)
360 unsigned char *PQescapeByteaConn(PGconn *conn,
361 const unsigned char *from,size_t from_length,size_t *to_length)
362 unsigned char *PQescapeBytea(const unsigned char *from,
363 size_t from_length,size_t *to_length)
364 unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length)
365 int PQsendQuery(PGconn *conn, const char *command)
366 int PQsendQueryParams(PGconn *conn,const char *command,
367 int nParams,const Oid *paramTypes,const char **paramValues,
368 const int *paramLengths,const int *paramFormats,int resultFormat)
369 int PQsendPrepare(PGconn *conn,const char *stmtName,
370 const char *query,int nParams,const Oid *paramTypes)
371 int PQsendQueryPrepared(PGconn *conn,const char *stmtName,
372 int nParams,const char **paramValues,
373 const int *paramLengths,const int *paramFormats,int resultFormat)
374 int PQsendDescribePrepared(PGconn *conn, const char *stmtName)
375 int PQsendDescribePortal(PGconn *conn, const char *portalName)
376 PGresult *PQgetResult(PGconn *conn)
377 int PQconsumeInput(PGconn *conn)
378 int PQisBusy(PGconn *conn)
379 int PQsetnonblocking(PGconn *conn, int arg)
380 int PQisnonblocking(const PGconn *conn)
381 int PQflush(PGconn *conn)
382 int PQsetSingleRowMode(PGconn *conn)
383 PGcancel *PQgetCancel(PGconn *conn)
384 void PQfreeCancel(PGcancel *cancel)
385 int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
386 int PQrequestCancel(PGconn *conn)
387 PGresult *PQfn(PGconn *conn,int fnid,int *result_buf,
388 int *result_len,int result_is_int,const PQArgBlock *args,int nargs)
389 PGnotify *PQnotifies(PGconn *conn)
390 int PQputCopyData(PGconn *conn,const char *buffer,int nbytes)
391 int PQputCopyEnd(PGconn *conn,const char *errormsg)
392 int PQgetCopyData(PGconn *conn,char **buffer,int async)
393 int PQgetline(PGconn *conn,char *buffer,int length)
394 int PQgetlineAsync(PGconn *conn,char *buffer,int bufsize)
395 int PQputline(PGconn *conn,const char *string)
396 int PQputnbytes(PGconn *conn,const char *buffer,int nbytes)
397 int PQendcopy(PGconn *conn)
398 int PQclientEncoding(const PGconn *conn)
399 char *pg_encoding_to_char(int encoding_id)
400 int PQsetClientEncoding(PGconn *conn, const char *encoding)
401 void PQtrace(PGconn *conn, FILE *stream)
402 void PQuntrace(PGconn *conn)
403 void PQfreemem(void *ptr)
404 void PQconninfoFree(PQconninfoOption *connOptions)
405 char *PQencryptPasswordConn(PGconn *conn, const char *passwd,
406 const char *user, const char *algorithm)
407 char *PQencryptPassword(const char *passwd, const char *user)
408 PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status)
409 int PQfireResultCreateEvents(PGconn *conn, PGresult *res)
410 PGresult *PQcopyResult(const PGresult *src, int flags)
411 int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs)
412 int PQsetvalue(PGresult *res, int tup_num, int field_num,
413 char *value, int len)
414 void *PQresultAlloc(PGresult *res, size_t nBytes)
415 int PQlibVersion(void)
416 PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn,
417 PQnoticeReceiver proc,void *arg)
418 PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn,
419 PQnoticeProcessor proc,void *arg)
420 void PQinitOpenSSL(int do_ssl, int do_crypto)
421 void PQinitSSL(int do_ssl)
422 int PQisthreadsafe(void)