各位同仁, 我用 PQprepare() 然后 PQexecPrepared() 插入的 integer 不是我输入的, 比如我输入 1, 在数据库中的值为 16777216, 怎么回事呢?
关键代码如下:
char sql[256];
sprintf(sql, "insert into table1(intId, indexId, double1, double2) values($1,$2,$3,$4)");
Oid types[4] = { 23, 23, 701, 701 };
PQprepare(con, "insert_table1", sql, sizeof(types)/sizeof(Oid), types);int paramLengths[4] = { sizeof(int), sizeof(int), sizeof(double), sizeof(double)};
int paramFormats[4] = { 1, 1, 1, 1};
int iParam[2];
double paramBuffer[2];
char * paramValues[4] = { (char*)&iParam[0], (char*)&iParam[1], (char*)¶mBuffer[0], (char*)¶mBuffer[1]};loop(i)...{
iParam[0] = i + 10;
iParam[1] = 1;
paramBuffer[0] = doublex1;
paramBuffer[1] = doublex2;
PQexecPrepared( con, "insert_table1", sizeof(paramValues)/sizeof(char *), paramValues, paramLengths, paramFormats, 0);
...
}
关键代码如下:
char sql[256];
sprintf(sql, "insert into table1(intId, indexId, double1, double2) values($1,$2,$3,$4)");
Oid types[4] = { 23, 23, 701, 701 };
PQprepare(con, "insert_table1", sql, sizeof(types)/sizeof(Oid), types);int paramLengths[4] = { sizeof(int), sizeof(int), sizeof(double), sizeof(double)};
int paramFormats[4] = { 1, 1, 1, 1};
int iParam[2];
double paramBuffer[2];
char * paramValues[4] = { (char*)&iParam[0], (char*)&iParam[1], (char*)¶mBuffer[0], (char*)¶mBuffer[1]};loop(i)...{
iParam[0] = i + 10;
iParam[1] = 1;
paramBuffer[0] = doublex1;
paramBuffer[1] = doublex2;
PQexecPrepared( con, "insert_table1", sizeof(paramValues)/sizeof(char *), paramValues, paramLengths, paramFormats, 0);
...
}
你将
完整的SQL语句显示出来看看
毛病就是ACMAIN_CHM 所说的那样,改成下列形式试试:
char * paramValues[4] = {"1", "1", "1.0", "1.0"};
const char *command,
int nParams,
const Oid *paramTypes,
const char * const *paramValues,
const int *paramLengths,
const int *paramFormats,
int resultFormat); PQexecParams is like PQexec, but offers additional functionality: parameter values can be specified separately from the command string proper, and query results can be requested in either text or binary format. PQexecParams is supported only in protocol 3.0 and later connections; it will fail when using protocol 2.0. The function arguments are: conn The connection object to send the command through.
command The SQL command string to be executed. If parameters are used, they are referred to in the command string as $1, $2, etc.
nParams The number of parameters supplied; it is the length of the arrays paramTypes[], paramValues[], paramLengths[], and paramFormats[]. (The array pointers can be NULL when nParams is zero.)
paramTypes[] Specifies, by OID, the data types to be assigned to the parameter symbols. If paramTypes is NULL, or any particular element in the array is zero, the server infers a data type for the parameter symbol in the same way it would do for an untyped literal string.
paramValues[] Specifies the actual values of the parameters. A null pointer in this array means the corresponding parameter is null; otherwise the pointer points to a zero-terminated text string (for text format) or binary data in the format expected by the server (for binary format).
paramLengths[] Specifies the actual data lengths of binary-format parameters. It is ignored for null parameters and text-format parameters. The array pointer can be null when there are no binary parameters.
paramFormats[] Specifies whether parameters are text (put a zero in the array entry for the corresponding parameter) or binary (put a one in the array entry for the corresponding parameter). If the array pointer is null then all parameters are presumed to be text strings. Values passed in binary format require knowledge of the internal representation expected by the backend. For example, integers must be passed in network byte order. Passing numeric values requires knowledge of the server storage format, as implemented in src/backend/utils/adt/numeric.c::numeric_send() and src/backend/utils/adt/numeric.c::numeric_recv().
resultFormat Specify zero to obtain results in text format, or one to obtain results in binary format. (There is not currently a provision to obtain different result columns in different formats, although that is possible in the underlying protocol.)