.. highlightlang:: us .. _pq_exec: pq_exec ======= .. index:: pq_exec .. us.tag pq_exec NOTREADYENGLISH New550 postgres :ref:`pq_exec` executes an SQL command. .. function:: res = pq_exec(db, ssCommand) .. us.return **Return Value** *res* is a pointer to access the query result (see :ref:`pq_nfields`, :ref:`pq_ntuples`, :ref:`pq_fname`, :ref:`pq_getvalue`). .. us.params **Parameters** .. uparam:: db *db* is the connection pointer returned by :ref:`pq_connectdb`. .. uparam:: ssCommand *ssCommand* is a string with one or more SQL command. .. us.example **Example** :: def test_create() { conn = pq_connectdb("host=localhost port=5432 user=postgres password=postgres") print pq_error_message(conn) print pq_status(conn) r = pq_exec(conn, "drop TABLE test1"); ssMsg = pq_result_error_message(r) pq_exec(conn, "CREATE TABLE test1 (i int4, f float, t text, b bytea)") pq_exec(conn, "BEGIN") for (i in 1:100) { s = sprintf("INSERT INTO test1 values(%d, %f, %s, '\001\002')", .. i, sin(i), sprintf("%f", sin(i))); pq_exec(conn, s) } pq_exec(conn, "END") pq_finish(conn) } def test_select() { conn = pq_connectdb("host=localhost port=5432 user=postgres password=postgres") print pq_error_message(conn) print pq_status(conn) res = pq_exec(conn, "SELECT i, f, t, b from test1 limit 10") print n = pq_nfields(res); print nrows = pq_ntuples(res); for (i in 1:n) { printf("%-15s", pq_fname(res, i-1)); } printf("\n"); for (i in 1:nrows) { for (j in 1:n) { printf("%-15s", pq_getvalue(res, i-1, j-1)); } printf("\n"); } pq_finish(conn) } .. us.history **History** .. list-table:: :header-rows: 1 * - Version - Description * - 5.5.0 - New .. seealso:: :ref:`overview-postgresql` :sub:`id-1281527`