pq_exec

pq_exec führt SQL-Anweisungen aus.

res = pq_exec(db, ssCommand)

Returnwert

res ist ein Pointer, über den auf das Abfrage-Ergebnis zugegriffen werden kann (siehe auch pq_nfields, pq_ntuples, pq_fname, pq_getvalue).

Parameter

db

db ist der von der Funktion pq_connectdb erzeugte Connection-Pointer.

ssCommand

ssCommand ist eine Zeichenkette mit einem oder mehreren SQL-Anweisungen.

Beispiel

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)
}

History

Version Beschreibung
5.5.0 Neu.

id-1281527