Python, ADO, VC++ 6

Keith Farmer farmerk at pacbell.net
Sun Jan 7 21:56:50 EST 2001


I've been attempting to create a Python module in C++ which uses ADO to
perform calculations against a database.  I've encountered a slight problem
which I can't figure out how to correct.  I suspect it's in some
housekeeping, since the test function works correctly the first time, but
crashes the second.  If anybody could tell me where my problem is and
(better yet) how it may be corrected, I'd really appreciate it.  My email
address is deoradh at yahoo.com.

For those who would suggest swig: swig refuses to run, complaining that it
can't find python.swg.  The documentation for the windows installation is
minimal at best, focussing on recompiling swig itself (there's already a
swig binary, so this isn't what I'm needing to do).  When given the -I
option to tell it the path where python.swg may be found, it generates the
wrapper source, but dies on compilation.  I'm fairly unimpressed with swig,
and feel there's a better way to go about this.  The source copied below is
the source I retreated back to after attempting swig.

PythonWin 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2000 Mark Hammond (MarkH at ActiveState.com) - see
'Help/About PythonWin' for further copyright information.
>>> import AstroMath
>>> AstroMath.countRows("workqueue")
12  [this is correct]
>>> AstroMath.countRows("workqueue")
[promptly crashes with a memory reference error]

.....


/* astromath.cpp -- astronomical calculations
*/

# import "c:\program files\common files\system\ado\msado15.dll" \
   rename ( "EOF", "adoEOF" ) no_namespace

# include "Python.h"

// Initialize OLE

struct InitOle {
 InitOle() { ::CoInitialize(NULL); }
 ~InitOle() { ::CoUninitialize(); }
} _init_InitOle_;

static PyObject *AstroMathError;

static PyObject *AstroMath_countRows(PyObject *self, PyObject *args)
/* ADO example to pass whole SQL clause */
{

 long cnt; /* Define return variable */

 char *table;
 if (!PyArg_ParseTuple(args, "s", &table))
  return NULL;

 HRESULT   hr  = S_OK;
 _RecordsetPtr Rs1  = NULL;

 _bstr_t Connect("DSN=Astronomy");
 _bstr_t Source(strcat("select count(*) as cnt from ", table));

 hr = Rs1.CreateInstance(__uuidof(Recordset));

 Rs1->Open(Source, Connect, adOpenForwardOnly, adLockReadOnly, -1);

 cnt = (long) Rs1->Fields->GetItem( _variant_t("cnt"))->Value;

 Rs1->Close();
 Rs1 = NULL;

 return Py_BuildValue("i", cnt); /* Return value */

}

static PyMethodDef AstroMathMethods[] = {
 { "countRows", AstroMath_countRows, METH_VARARGS },
 { NULL, NULL }    /* Sentinel */
};

#ifdef MS_WIN32
__declspec(dllexport)
#endif

void initAstroMath()
{
 PyObject *m, *d;

 m = Py_InitModule("AstroMath", AstroMathMethods);
 d = PyModule_GetDict(m);
 AstroMathError = PyErr_NewException("AstroMath.error", NULL, NULL);
 PyDict_SetItemString(d, "error", AstroMathError);
}







More information about the Python-list mailing list