bonjour,

voila, j'essaie de faire un simple petit programme avec des cursors et des variables dynamique (les $X) en C. le but etant que j'arrive a changer la variable sans refaire le prepare .

je fait donc
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
 
 PQprepare  (conn,
                       "my_cursor",
                       "DECLARE myportal CURSOR FOR select * from tab_3 WHERE COL_3 = $1",
                       1,       /* one param */
                       NULL);

une seul fois
et puis je fais un
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
 
  paramValues[0]= "zzze";
    res = PQexecPrepared(conn,
                         "my_cursor",
                         1,
                         paramValues,
                         NULL,
                         NULL,
                         1);
ensuite je fetch (tout fonctionne)
et puis ben j'aimerais bien changer la valeur du param
je fais donc un
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
 
    paramValues[0]= "ee";
    res = PQexecPrepared(conn,
                       "my_cursor",
                        1,
                        paramValues,
                        NULL,
                        NULL,
                        1);
et la cela foire. j'ai le message
ERREUR: le curseur ½ myportal ╗ existe dÚjÓ
or je sais qu'il existe, c'est le but .

qqn aurait il une idée de pourquoi??? et comment resoudre

merci a++

voici le code complet (basé sur un example)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
 
/*
 * testlibpq.c
 *
 *      Test the C version of libpq, the PostgreSQL frontend library.
 */
#include <stdio.h>
#include <stdlib.h>
#include "libpq-fe.h"
 
static void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}
 
PGresult* FetchFirst(PGconn * conn){
    PGresult* res = PQexec(conn, "FETCH FIRST in myportal");
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        fprintf(stderr, "FETCH  failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }
    return res;
}
 
 
int
main(int argc, char **argv)
{
 
    PGconn     *conn;
    PGresult   *res;
    int         nFields;
    int         i,
                j;
    const char *paramValues[1];
 
 
    conn = PQsetdbLogin("localhost", NULL, NULL,NULL, "dbu", "root","ouvretoi");
 
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "Connection to database failed: %s",
                PQerrorMessage(conn));
        exit_nicely(conn);
    }
 
    res = PQexec(conn, "BEGIN");
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "BEGIN command failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }
     PQclear(res);
     res =  PQprepare  (conn,
                       "my_cursor",
                       "DECLARE myportal CURSOR FOR select * from tab_3 WHERE COL_3 = $1",
                       1,       /* one param */
                       NULL); 
 
    PQclear(res);
 
    paramValues[0]= "zzze";
    res = PQexecPrepared(conn,
                         "my_cursor",
                         1,
                         paramValues,
                         NULL,
                         NULL,
                         1);
 
    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        fprintf(stderr, "DECLARE CURSOR failed: %s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }
    PQclear(res);
 
 
    res = FetchFirst(conn);
 
    nFields = PQnfields(res);
    for (i = 0; i < nFields; i++)
        printf("%-15s", PQfname(res, i));
    printf("\n\n");
 
    while(PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
    {
            for (j = 0; j < nFields; j++)
                printf("%-15s", PQgetvalue(res, 0, j));
            printf("\n");
            PQclear(res);
            res = PQexec(conn, "FETCH NEXT in myportal");
    }
    PQclear(res);
 
 
    paramValues[0]= "ee";
    res = PQexecPrepared(conn,
                       "my_cursor",
                        1,
                        paramValues,
                        NULL,
                        NULL,
                        1);
 
    fprintf(stderr, " %s", PQerrorMessage(conn));
    res = FetchFirst(conn);
 
 
    /* first, print out the attribute names */
    nFields = PQnfields(res);
    for (i = 0; i < nFields; i++)
        printf("%-15s", PQfname(res, i));
    printf("\n\n");
 
    while(PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
    {
            for (j = 0; j < nFields; j++)
                printf("%-15s", PQgetvalue(res, 0, j));
            printf("\n");
            PQclear(res);
       res = PQexec(conn, "FETCH NEXT in myportal");
    }
    PQclear(res);
 
 
    res = PQexec(conn, "CLOSE myportal");
    PQclear(res);
 
    /* end the transaction */
    res = PQexec(conn, "END");
    PQclear(res);
 
    /* close the connection to the database and cleanup */
    PQfinish(conn);
 
    return 0;
}