I am using python. Hooray for new fangled scripting glory. 
My business is an mssql shop.  meh.
I set up pyodbc. I run a simple insert statement. Sql Server Profiler logs the following (among other tidbits).

declare @p1 int
set @p1=1
exec sp_prepare @p1 output,N'@P1 VARCHAR(11)',N'insert into schemax.iptable (IPAddress) values(@P1)',1
select @p1

Very simple insert me thinks.
I want to try new orm hotness that is SQL Alchemy.
Again with the very simple test. SQL Server Profiler logs the following:

declare @p1 int
set @p1=13808
exec sp_prepare @p1 output,NULL,N'',1
select @p1

See the subtle difference?

No errors are ever thrown. It runs its little heart out. But no data is every actually inserted into the database.

I loathe odbc.  

Also, if your ever perusing the sparse documentation available on pyodbc, and see all those helpful quick examples be sure to call the commit() method which they do not show or set the autocommit flag to true.

update: my woes on the sqlalchemy-devel list here.

5 Responses to “mssql – odbc – python hell”

  1. meantheory said

    Quite distracted with many other things but I still play with this problem on occasion. I believe this thread may be related: http://code.google.com/p/django-pyodbc/issues/detail?id=9

  2. meantheory said

    Not sure if this is a related problem for me or not but this was an interesting tid bit from http://www.easysoft.com/developer/languages/python/pyodbc.html:

    “Python can be built as either UCS2 or UCS4, which defines Python’s internal storage format for Unicode strings. pyodbc does not do any conversion between Unicode encoding schemes. So, pyodbc will pass UCS-2 encoded Unicode data to Python, even if Python is expecting UCS-4 (as will be the case for a UCS4 build of Python).

    To test whether you are using a UCS2 or UCS4 build of Python, run:

    python -c “import sys;print(sys.maxunicode<66000)and’UCS2′or’UCS4′""

  3. meantheory said

    possible sqlalchemy workaround inside the following ticket.
    http://www.sqlalchemy.org/trac/ticket/1312

    this should get around pyodbc problems by avoiding unicode.

Leave a Reply