473,394 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Problem with odbc (pywin32) and unicode

Hi all

I am using odbc from pywin32 to connect to MS SQL Server. I am changing
my program from the old (incorrect) style of embedding values in the
SQL command to the new (correct) style of passing the values as
parameters. I have hit a problem.

The following all work -
cur.execute("select * from users where userid = '%s'" % 'frank') #
old style
cur.execute("select * from users where userid = '%s'" % u'frank')
# old style
cur.execute("select * from users where userid = ?", ['frank']) #
new style

This does not work -
cur.execute("select * from users where userid = ?", [u'frank']) #
new style

I get the following error -
OdbcError: Found an insert row that didn't have 1 columns [sic]

It is the combination of new style and unicode that does not work.

I realise that odbc is not fully DB-API compliant, but I have got away
with it up to now. Does anyone know of a solution to this problem?

Thanks

Frank Millman

ps to Fredrik, who just added this to a recent post -

"(just curious, but from where do people get the idea that arbitrary
data
just have to be inserted into the the SQL statement text all the time?
is
this some PHP misfeature?) "

I can't answer for anyone else, but this is my excuse.

Sequence of events -

learn a bit about Python - play in interpreter, write simple scripts,
etc.
learn a bit about SQL - play in an interactive session, run some
commands, etc.
try to combine the two - wow, it works!
read in the DB-API about parameter passing - frankly, don't
understand it.
therefore stick with what works for me.

Parameter passing seems to be one of those things that, if you
understand it, you can't imagine that there is anyone out there that
does not understand it, and therefore you think that the documentation
is perfectly clear. However, if you are one of those unfortunate souls
who have never come across such a thing, the docs simply do do not
spell it out clearly enough, so one's reaction is more 'huh?' than
'aha!'

Jan 27 '06 #1
5 2342
Frank Millman wrote:
Hi all

I am using odbc from pywin32 to connect to MS SQL Server. I am changing
my program from the old (incorrect) style of embedding values in the
SQL command to the new (correct) style of passing the values as
parameters. I have hit a problem.

The following all work -
cur.execute("select * from users where userid = '%s'" % 'frank') #
old style
cur.execute("select * from users where userid = '%s'" % u'frank')
# old style
cur.execute("select * from users where userid = ?", ['frank']) #
new style

This does not work -
cur.execute("select * from users where userid = ?", [u'frank']) #
new style

I get the following error -
OdbcError: Found an insert row that didn't have 1 columns [sic]


To me it looks as if your problem stems from not encoding the parameter as a
string, but pasing a unicode-object that maybe internally causes trouble -
and a misleading error message.

That things work in your second old-style case comes from youprobably not
coming from a country where non-ascii characters are usual. Otherwise you'd
have seen that e.g.

"select * from users where userid = '%s'" % u'Martin von Löwis'

will produce the notorious

UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position
12: ordinal not in range(128)

So it is silently converted to a string by the %-operator using the ascii
codec.

So I suggest you try this:

cur.execute("select * from users where userid = ?",
[u'frank'.encode("utf-8"])
If that works, we know the problem. Make sure that your DB is expecting
UTF-8, or use the encoding it wants instead!

Regards,

Diez

Jan 27 '06 #2

Diez B. Roggisch wrote:
Frank Millman wrote:

This does not work -
cur.execute("select * from users where userid = ?", [u'frank']) #
new style

I get the following error -
OdbcError: Found an insert row that didn't have 1 columns [sic]


To me it looks as if your problem stems from not encoding the parameter as a
string, but pasing a unicode-object that maybe internally causes trouble -
and a misleading error message.

So I suggest you try this:

cur.execute("select * from users where userid = ?",
[u'frank'.encode("utf-8"])
If that works, we know the problem. Make sure that your DB is expecting
UTF-8, or use the encoding it wants instead!


Thanks, Diez - I tried it and it works.

Unicode is one of those grey areas that I know I will have to try to
understand one day, but I am putting off that day as long as possible!

When you say it causes trouble internally, do you mean internal to the
odbc module? Is it something that should be reported as a bug?

What should I do in the meantime? Make encode("utf-8") a permanent
feature of my program?

BTW, the reason I am getting unicode objects is that I use wxPython as
my front end. You can opt for a unicode or a non-unicode build. I
decided to go for unicode to cater for future requirements. This is the
first time it has bitten me - I am sure it will not be the last.

Many thanks for your help

Frank

Jan 27 '06 #3
> Unicode is one of those grey areas that I know I will have to try to
understand one day, but I am putting off that day as long as possible!
I suggest you better start right away instead of stumbling over it all the
time. The most problems in that field don't come from the inherent
complexity of the matter itself, but form the profound lack of
understanding of developers thereof - worldwide, btw. You are not alone, as
Mr. Jackson sang...

When you say it causes trouble internally, do you mean internal to the
odbc module? Is it something that should be reported as a bug?
Not sure, but I guess: yes.
What should I do in the meantime? Make encode("utf-8") a permanent
feature of my program?


You should do it in a way that allows you to either pass the encoding
explicitly, or make it some sort of configurable thing, in the same spot
where you configure your connection settings, as these two are related.

Diez
Jan 27 '06 #4

Diez B. Roggisch wrote:
Unicode is one of those grey areas that I know I will have to try to
understand one day, but I am putting off that day as long as possible!


I suggest you better start right away instead of stumbling over it all the
time. The most problems in that field don't come from the inherent
complexity of the matter itself, but form the profound lack of
understanding of developers thereof - worldwide, btw. You are not alone, as
Mr. Jackson sang...


In other words, that day has arrived. I will roll my sleeves up and get
stuck into the docs.

When you say it causes trouble internally, do you mean internal to the
odbc module? Is it something that should be reported as a bug?


Not sure, but I guess: yes.
What should I do in the meantime? Make encode("utf-8") a permanent
feature of my program?


You should do it in a way that allows you to either pass the encoding
explicitly, or make it some sort of configurable thing, in the same spot
where you configure your connection settings, as these two are related.

Diez


Many thanks for your help, Diez - I will take it from here.

Frank

Jan 29 '06 #5

Frank Millman wrote:
Hi all

I am using odbc from pywin32 to connect to MS SQL Server. I am changing
my program from the old (incorrect) style of embedding values in the
SQL command to the new (correct) style of passing the values as
parameters. I have hit a problem.

The following all work -
cur.execute("select * from users where userid = '%s'" % 'frank') #
old style
cur.execute("select * from users where userid = '%s'" % u'frank')
# old style
cur.execute("select * from users where userid = ?", ['frank']) #
new style

This does not work -
cur.execute("select * from users where userid = ?", [u'frank']) #
new style

I get the following error -
OdbcError: Found an insert row that didn't have 1 columns [sic]

It is the combination of new style and unicode that does not work.

I realise that odbc is not fully DB-API compliant, but I have got away
with it up to now. Does anyone know of a solution to this problem?

Thanks

Frank Millman


I am pleased to report that this has been fixed.

I have been liaising with Mark Hammond, and he has sent me a revised
version which behaves correctly.

This is from Mark -

"I'd be happy for you to mention this fix will be in build 208 - I just
can't tell you when that will be :)"

Frank

Feb 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Jordan | last post by:
I'm hoping that someone here can give me some insight into a problem I'm running into with Python, pywin32 and Excel. All-in-all using Python and pywin32 is great but I've run into a strange...
4
by: Bob Chapman | last post by:
On two different Windows 98SE Systems, I have attempted to upgrade working Python 2.3.3 and win32all installations to Python 2.3.4. After what appeared to be successful installations of...
13
by: Graham | last post by:
I need a SQL Server or ODBC package for Python. Can anyone help please? Have search the Python packages under Database and there is no reference to either SQL Server or ODBC. Thanks Graham
0
by: AlessanBar | last post by:
Hello Friends !! I have a strange problem, and I need to know what would be the source of this. I have a laptop computer with the following configuration: Pentium III Brand : Toshiba Speed :...
2
by: Len Kowalik | last post by:
MSAccess 2000, SQL-Server 97 I am getting a permission denied response from an attempt to execute a stored proc, which has public access revoked and app role access, on the last statement of the...
2
by: SpoonfulofTactic | last post by:
I am building a Blog for my own person use, and I would like to be able to use unicode characters outside the range of the ascii set. However, when I use odbc to access table data, Access returns...
1
by: lblr33 | last post by:
I would like to connect to an Oracle database. In python version 2.1 I used the following code and it worked fine. When I run the code in version 2.4 it says that the odbc moduled does not exist....
4
by: phirestorm | last post by:
Hopefully someone can help me with an issue i am having. First off let me say that i have little to no experience with MS SQL or SQL in general. We have a Windows 2003 server running MSSQL (i...
4
by: Xavier | last post by:
Hi, I try to access to a Bluetooth GPS data-logger with Python. I use pySerial. Sending and receiving little messages (~100 char) works fine. However, when I ask the GPS to dump the trails,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.