116{
117 const char *conninfo;
120 const char *paramValues[1];
121 int paramLengths[1];
122 int paramFormats[1];
123 uint32_t binaryIntVal;
124
125 /*
126 * If the user supplies a parameter on the command line, use it as the
127 * conninfo string; otherwise default to setting dbname=postgres and using
128 * environment variables or defaults for all other connection parameters.
129 */
130 if (argc > 1)
131 conninfo = argv[1];
132 else
133 conninfo = "dbname = postgres";
134
135 /* Make a connection to the database */
137
138 /* Check to see that the backend connection was successfully made */
140 {
143 }
144
145 /* Set always-secure search path, so malicious users can't take control. */
146 res =
PQexec(
conn,
"SET search_path = testlibpq3");
148 {
152 }
154
155 /*
156 * The point of this program is to illustrate use of PQexecParams() with
157 * out-of-line parameters, as well as binary transmission of data.
158 *
159 * This first example transmits the parameters as text, but receives the
160 * results in binary format. By using out-of-line parameters we can avoid
161 * a lot of tedious mucking about with quoting and escaping, even though
162 * the data is text. Notice how we don't have to do anything special with
163 * the quote mark in the parameter value.
164 */
165
166 /* Here is our out-of-line parameter value */
167 paramValues[0] = "joe's place";
168
170 "SELECT * FROM test1 WHERE t = 1ドル",
171 1, /* one param */
172 NULL, /* let the backend deduce param type */
173 paramValues,
174 NULL, /* don't need param lengths since text */
175 NULL, /* default to all text params */
176 1); /* ask for binary results */
177
179 {
183 }
184
186
188
189 /*
190 * In this second example we transmit an integer parameter in binary form,
191 * and again retrieve the results in binary form.
192 *
193 * Although we tell PQexecParams we are letting the backend deduce
194 * parameter type, we really force the decision by casting the parameter
195 * symbol in the query text. This is a good safety measure when sending
196 * binary parameters.
197 */
198
199 /* Convert integer value "2" to network byte order */
200 binaryIntVal = htonl((uint32_t) 2);
201
202 /* Set up parameter arrays for PQexecParams */
203 paramValues[0] = (char *) &binaryIntVal;
204 paramLengths[0] = sizeof(binaryIntVal);
205 paramFormats[0] = 1; /* binary */
206
208 "SELECT * FROM test1 WHERE i = 1ドル::int4",
209 1, /* one param */
210 NULL, /* let the backend deduce param type */
211 paramValues,
212 paramLengths,
213 paramFormats,
214 1); /* ask for binary results */
215
217 {
221 }
222
224
226
227 /* close the connection to the database and cleanup */
229
230 return 0;
231}
#define fprintf(file, fmt, msg)
PGconn * PQconnectdb(const char *conninfo)
ConnStatusType PQstatus(const PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
PGresult * PQexecParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char *const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat)
PGresult * PQexec(PGconn *conn, const char *query)
static void exit_nicely(PGconn *conn)
static void show_binary_results(PGresult *res)