On Sun, Mar 11, 2007 at 09:06:16PM -0700, Steve Langasek wrote:

> On Sun, Mar 11, 2007 at 07:30:04PM +0000, Enrico Zini wrote:
> > http://msdn2.microsoft.com/en-us/library/ms714556.aspx
> > says that SQL_C_SLONG represents a long int.
> 
> Microsoft's docs are totally useless for correctly implementing ODBC on
> 64-bit architectures.  Please explain the /problem/ you're trying to solve,
> preferably with a reproducible use case.

Sorry.  What's a good authoritative API reference then?

I'm attaching a C program that reproduces the problem: it prints:

  Foo is -4294967295

where it should instead print:

  Foo is 1



Ciao,

Enrico

-- 
GPG key: 1024D/797EBFAB 2000-12-05 Enrico Zini <[EMAIL PROTECTED]>
#include <stdio.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>

void check(SQLSMALLINT handletype, SQLHSTMT stm, int res)
{
	if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO))
	{
		static const int strsize = 200;
		char stat[10], msg[strsize];
		SQLINTEGER err;
		SQLSMALLINT mlen;
		SQLGetDiagRec(handletype, stm, 1, (unsigned char*)stat, &err, (unsigned char*)msg, strsize, &mlen);
		if (mlen > strsize) mlen = strsize;
		fprintf(stderr, "Error %d %s: %.*s\n", err, stat, mlen, msg);
		exit(1);
	}
}

void checkenv(SQLHENV stm, int res)
{
	check(SQL_HANDLE_ENV, stm, res);
}
void checkdbc(SQLHSTMT stm, int res)
{
	check(SQL_HANDLE_DBC, stm, res);
}
void checkstm(SQLHSTMT stm, int res)
{
	check(SQL_HANDLE_STMT, stm, res);
}

int main()
{
	SQLHENV dba_od_env;
	SQLHDBC od_conn;
	SQLHSTMT stm;
	long foo;

	SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &dba_od_env);
	checkenv(dba_od_env, SQLSetEnvAttr(dba_od_env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0));

	SQLAllocHandle(SQL_HANDLE_DBC, dba_od_env, &od_conn);

	printf("Connecting\n");
	checkdbc(od_conn, SQLConnect(od_conn, "test", SQL_NTS, "enrico", SQL_NTS, "", SQL_NTS));

	SQLAllocHandle(SQL_HANDLE_STMT, od_conn, &stm);

	printf("Creating table\n");
	checkstm(stm, SQLExecDirect(stm, "CREATE TABLE foo (id INTEGER)", SQL_NTS));
	printf("Populating table\n");
	checkstm(stm, SQLExecDirect(stm, "INSERT INTO foo VALUES (1)", SQL_NTS));

	/* Init foo with some garbage, to catch the case in which it's filled
	 * in only partially */
	foo=-1;

	checkstm(stm, SQLBindCol(stm, 1, SQL_C_SLONG, &foo, sizeof(foo), 0));
	printf("Querying table\n");
	checkstm(stm, SQLExecDirect(stm, "SELECT id FROM foo", SQL_NTS));

	if (SQLFetch(stm) == SQL_NO_DATA)
		fprintf(stderr, "No results for foo\n");

	printf("Foo is %ld\n", foo);

	checkstm(stm, SQLCloseCursor(stm));

	printf("Dropping table\n");
	checkstm(stm, SQLExecDirect(stm, "DROP TABLE foo", SQL_NTS));

	return 0;
}

Attachment: signature.asc
Description: Digital signature

Reply via email to