[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Inserting NULL values with pymssql

Steve Holden

2/21/2008 11:48:00 PM

Jayson Barley wrote:
> I am attempting to insert NULL values into a database. I have tried to
> do this in multiple ways without any success, see below, and haven't
> been able to find anything through Google to help out. I am hoping that
> I am just overlooking something or that it is a rookie mistake. Below is
> a test I came up with to prove this. I am on Windows XP using Python 2.5
> and pymssql-0.8.0-py2.5.
>
> CREATE TABLE [dbo].[Test](
> [test] [varchar](50) NULL,
> [inttest] [int] NULL
> ) ON [PRIMARY]
>
> 1.
> import pymssql
>
> TestDB =
> pymssql.connect(host='Test',user='test',password='test',database='test')
> cursor = TestDB.cursor()
> query = """INSERT INTO test.dbo.test VALUES (?, ?);"""
> cursor.execute(query,('',''))
> Returns
>
> Traceback (most recent call last):
> File "C:\Test\db_info\test.py", line 6, in <module>
> cursor.execute(query,('',''))
> File "C:\Python25\lib\site-packages\pymssql.py", line 126, in execute
> self.executemany(operation, (params,))
> File "C:\Python25\lib\site-packages\pymssql.py", line 152, in executemany
> raise DatabaseError, "internal error: %s" % self.__source.errmsg()
> pymssql.DatabaseError: internal error: None
>
> 2.
> import pymssql
>
> TestDB =
> pymssql.connect(host='Test',user='test',password='test',database='test')
> cursor = TestDB.cursor()
> query = """INSERT INTO test.dbo.test VALUES (?, ?);"""
> cursor.execute(query,('',None))
>
> Returns
>
> Traceback (most recent call last):
> File "C:\Test\db_info\test.py", line 8, in <module>
> cursor.execute(query,('',None))
> File "C:\Python25\lib\site-packages\pymssql.py", line 126, in execute
> self.executemany(operation, (params,))
> File "C:\Python25\lib\site-packages\pymssql.py", line 152, in executemany
> raise DatabaseError, "internal error: %s" % self.__source.errmsg()
> pymssql.DatabaseError: internal error: None
>
> 3.
> import pymssql
>
> TestDB =
> pymssql.connect(host='Test',user='test',password='test',database='test')
> cursor = TestDB.cursor()
> query = """INSERT INTO test.dbo.test VALUES (?, ?);"""
> cursor.execute(query,('','NULL'))
> Returns
>
> Traceback (most recent call last):
> File "C:\Test\db_info\test.py", line 6, in <module>
> cursor.execute(query,('','NULL'))
> File "C:\Python25\lib\site-packages\pymssql.py", line 126, in execute
> self.executemany(operation, (params,))
> File "C:\Python25\lib\site-packages\pymssql.py", line 152, in executemany
> raise DatabaseError, "internal error: %s" % self.__source.errmsg()
> pymssql.DatabaseError: internal error: None
>
> I am wondering what I am missing that is preventing me from inserting a
> NULL. I can perform the INSERT in Server Management Studio the problem
> only exists in the Python script. If anyone can point me to a resource
> that I may have missed or a flaw in my script I would really appreciate it.
>
What you want is two NULL data values as the second argument to execute():

cursor.execute(query,(None, None))

> I also forgot to mention that this...
>
> import pymssql
>
> TestDB = pymssql.connect(host='Test',user='test',password='test',database='test')
> cursor = TestDB.cursor()
> query = """INSERT INTO test.dbo.test (test) VALUES ('%s');"""
> cursor.execute(query,(None))
>
> works. While
>
That's a very naughty database module. It should complain, since you
didn't provide a tuple as the second argument to execute().

> import pymssql
>
> TestDB = pymssql.connect(host='Test',user='test',password='test',database='test')
> cursor = TestDB.cursor()
> query = """INSERT INTO test.dbo.test (inttest) VALUES ('%d');"""
> cursor.execute(query,(None))
>
> doesn't work and returns
>
> Traceback (most recent call last):
> File "C:\Test\db_info\test.py", line 6, in <module>
> cursor.execute(query,(None))
> File "C:\Python25\lib\site-packages\pymssql.py", line 126, in execute
> self.executemany(operation, (params,))
> File "C:\Python25\lib\site-packages\pymssql.py", line 152, in executemany
> raise DatabaseError, "internal error: %s" % self.__source.errmsg()
> pymssql.DatabaseError: internal error: SQL Server message 245, severity 16, state 1, line 1:
> Conversion failed when converting the varchar value '%d' to data type int.
> DB-Lib error message 10007, severity 5:
> General SQL Server error: Check messages from the SQL Server.

I don't know what pymssql's paramstyle is, but I suspect it requires a
"%s" far all parameters regardless of their data type. That's SQL Server
complaining about the %d, pymssql having failed to recognize it as a
parameter marker and passed it through to the engine.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.hold...

42 Answers

William Black

6/14/2014 7:40:00 PM

0

On Sat, 14 Jun 2014 11:25:13 -0700 (PDT), jerry kraus
<jkraus1999@gmail.com> wrote:


>The key point is that there is no way the Soviets can survive with Leningrad taken early in the campaign,

Why?

The streetfighting in Leningrad will make Stalingrad look like a
cakewalk.

Jerry Kraus

6/14/2014 7:52:00 PM

0

On Saturday, June 14, 2014 2:39:51 PM UTC-5, Bill wrote:
> On Sat, 14 Jun 2014 11:25:13 -0700 (PDT), jerry kraus
>
> <jkraus1999@gmail.com> wrote:
>
>
>
>
>
> >The key point is that there is no way the Soviets can survive with Leningrad taken early in the campaign,
>
>
>
> Why?
>
>
>
> The streetfighting in Leningrad will make Stalingrad look like a
>
> cakewalk.

Doesn't really matter if you can easily and safely transport all the troops and material you want directly from Germany via the Baltic.


Stalingrad was rather far from Germany, you know. Supply line troubles, you know.

William Black

6/14/2014 8:30:00 PM

0

On Sat, 14 Jun 2014 12:52:05 -0700 (PDT), jerry kraus
<jkraus1999@gmail.com> wrote:

>On Saturday, June 14, 2014 2:39:51 PM UTC-5, Bill wrote:
>> On Sat, 14 Jun 2014 11:25:13 -0700 (PDT), jerry kraus
>>
>> <jkraus1999@gmail.com> wrote:
>>
>>
>>
>>
>>
>> >The key point is that there is no way the Soviets can survive with Leningrad taken early in the campaign,
>>
>>
>>
>> Why?
>>
>>
>>
>> The streetfighting in Leningrad will make Stalingrad look like a
>>
>> cakewalk.
>
>Doesn't really matter if you can easily and safely transport all the troops and material you want directly from Germany via the Baltic.

Actually it does.

The Soviet navy may be something of a joke at that time, but Soviet
submarines operated in the Baltic until the German mine laying
programme of 1943.

Plus, the Soviet Union had this distressing habit of arming the
civilian population. Leningrad is a sight bigger than Stalingrad with
a much bigger population and a far greater commitment to Communism.

The Germans are going to spend a considerable time digging them out,
if they survive that first winter...

>Stalingrad was rather far from Germany, you know. Supply line troubles, you know.

The weather is better there though.

If you think the winter in Stalingrad was cold you ain't seen nothing
yet...

(-35C vice -26C)

Jerry Kraus

6/14/2014 8:38:00 PM

0

On Saturday, June 14, 2014 3:29:38 PM UTC-5, Bill wrote:
> On Sat, 14 Jun 2014 12:52:05 -0700 (PDT), jerry kraus
>
> <jkraus1999@gmail.com> wrote:
>
>
>
> >On Saturday, June 14, 2014 2:39:51 PM UTC-5, Bill wrote:
>
> >> On Sat, 14 Jun 2014 11:25:13 -0700 (PDT), jerry kraus
>
> >>
>
> >> <jkraus1999@gmail.com> wrote:
>
> >>
>
> >>
>
> >>
>
> >>
>
> >>
>
> >> >The key point is that there is no way the Soviets can survive with Leningrad taken early in the campaign,
>
> >>
>
> >>
>
> >>
>
> >> Why?
>
> >>
>
> >>
>
> >>
>
> >> The streetfighting in Leningrad will make Stalingrad look like a
>
> >>
>
> >> cakewalk.
>
> >
>
> >Doesn't really matter if you can easily and safely transport all the troops and material you want directly from Germany via the Baltic.
>
>
>
> Actually it does.
>
>
>
> The Soviet navy may be something of a joke at that time, but Soviet
>
> submarines operated in the Baltic until the German mine laying
>
> programme of 1943.
>
>
>
> Plus, the Soviet Union had this distressing habit of arming the
>
> civilian population. Leningrad is a sight bigger than Stalingrad with
>
> a much bigger population and a far greater commitment to Communism.
>
>
>
> The Germans are going to spend a considerable time digging them out,
>
> if they survive that first winter...
>
>
>
> >Stalingrad was rather far from Germany, you know. Supply line troubles, you know.
>
>
>
> The weather is better there though.
>
>
>
> If you think the winter in Stalingrad was cold you ain't seen nothing
>
> yet...
>
>
>
> (-35C vice -26C)

Well, I guess you know better than Stalin did, Bill. He seemed rather concerned about this issue. Fought the Russo-Finnish War over it, actually.

The Horny Goat

6/14/2014 10:49:00 PM

0

On Sat, 14 Jun 2014 11:25:13 -0700 (PDT), jerry kraus
<jkraus1999@gmail.com> wrote:

>OK Rich, the Finnish Border was in the suburbs of Leningrad. Are you happy?
>
>The key point is that there is no way the Soviets can survive with Leningrad taken early in the campaign, and, there is no way it won't be under the circumstances I describe.

If you're suggesting holding Leningrad helps German logistics you're
undoubtedly right to a limited extent. If you're suggesting it's
essential for its production capacity then clearly not since the
Soviets were essentially without Leningrad for 2 1/2 years. It was
basically a giant artillery target for most of the 900+ days.

Certainly a German rail link to Finland would have helped -but this
would not of itself be a war winner. Even if it led to the swift
capture of Murmansk it's STILL not a war winner.

>Given this fact, Hitler will do whatever it takes to persuade Finland to cooperate. And, he has considerable power at this stage. And, Stalin is afraid of him, at this stage.
>
>Also, given no Russo-Finnish War, we are assuming a much dumber Stalin than in OTL.

Why would you assume that? Given the Soviet personnel losses in 1941
one could scarcely do worse than OTL. It wasn't just 1941 either -
look at the offensive that led to the capture of General AA Vlasov for
a prime example of the Red Army at its worst.

I have no doubt that Russia came much much closer to defeat than
Britain did - but no question German intelligence was deficient in
1941 - based on German pre-invasion estimates Russia had run out of
troops by October 1941. Now that was obviously not true so the pre-war
assessments were dead wrong.

Hitler was expecting to win in Russia in 1941 and was unprepared for a
winter campaign - and 500,000 Germans were casualties as a result.
German casualties in the first 12 months of the war were higher than
in the second 12 months - but the critical fact was that Russia COULD
survive the casualties while Germany couldn't particularly given the
losses elsewhere. (The German losses in Tunis were close to those of
the Stalingrad campaign - my personal opinion is that Rommel COULD
have supplied 100,000 more troops in North Africa and that that likely
would have meant the conquest of Egypt but Hitler put far more than
that into Tunisia with the result that it impaired the German defence
of Italy)

The Horny Goat

6/14/2014 10:55:00 PM

0

On Sat, 14 Jun 2014 21:29:38 +0100, Bill <blackusenet@gmail.com>
wrote:

>The Germans are going to spend a considerable time digging them out,
>if they survive that first winter...
>
>>Stalingrad was rather far from Germany, you know. Supply line troubles, you know.
>
>The weather is better there though.
>
>If you think the winter in Stalingrad was cold you ain't seen nothing
>yet...
>
>(-35C vice -26C)

One of my great memories of my 4 years of living in Winnipeg was an
afternoon spent in the reading room of the public library there
reading a newspaper after finishing a book on the 1941-42 winter
campaign which had the interesting feature of having the temperature
at Moscow for each day printed on the top right hand corner of the
page from October onwards.

And realizing that Winnipeg was 5-10 degrees COLDER that day. (I
remember going into a 'warm-up shack' where it was -25C inside....)

Now given the primary outdoor occupation of Winnipeggers in January is
getting in out of the cold it's rather appalling to think people
actually fought in such conditions but at least one somewhat
understands.

As you say, Stalingrad in November to January 1942/3 was cold enough
but not nearly in that league.

Jerry Kraus

6/14/2014 11:16:00 PM

0

On Saturday, June 14, 2014 5:49:25 PM UTC-5, The Horny Goat wrote:
> On Sat, 14 Jun 2014 11:25:13 -0700 (PDT), jerry kraus
>
> <jkraus1999@gmail.com> wrote:
>
>
>
> >OK Rich, the Finnish Border was in the suburbs of Leningrad. Are you happy?
>
> >
>
> >The key point is that there is no way the Soviets can survive with Leningrad taken early in the campaign, and, there is no way it won't be under the circumstances I describe.
>
>
>
> If you're suggesting holding Leningrad helps German logistics you're
>
> undoubtedly right to a limited extent. If you're suggesting it's
>
> essential for its production capacity then clearly not since the
>
> Soviets were essentially without Leningrad for 2 1/2 years. It was
>
> basically a giant artillery target for most of the 900+ days.
>
>
>
> Certainly a German rail link to Finland would have helped -but this
>
> would not of itself be a war winner. Even if it led to the swift
>
> capture of Murmansk it's STILL not a war winner.
>
>
>
>

Leningrad wasn't essential for the Russians -- it was essential for the Germans. Leningrad gave the Russians no particular advantage for defense -- but it's possession gives the Germans virtually unlimited safe transport capacity into the heart of North Russia. The Germans actually did pretty well without this. With it, they couldn't have lost. The Russians would have had too many fronts to defend, and the Germans had superior mobile armor, certainly at the start of the war.

Moscow will certainly fall before winter comes on, with Leningrad under control from early in the campaign. It almost did in OTL. It now can be attacked quickly from the North, as well as from the West. There will be no way to supply the Russians from Murmansk, they will be cut off from British supplies by Nazi troops in North Russia. Stalin's Siberian troops will have to be brought in before winter, and they will therefore be less effective.

While the Soviets will certainly still control Siberia, they will have most of their population, and virtually all of their heavy industry and readily retrievable resources -- including oil -- under Nazi control before the onset of winter.

William Black

6/14/2014 11:51:00 PM

0

On Sat, 14 Jun 2014 13:38:20 -0700 (PDT), jerry kraus
<jkraus1999@gmail.com> wrote:

>On Saturday, June 14, 2014 3:29:38 PM UTC-5, Bill wrote:


>
>Well, I guess you know better than Stalin did, Bill. He seemed rather concerned about this issue. Fought the Russo-Finnish War over it, actually.

Yeah, and he retreated from Leningrad as it was indefensible... NOT!

Phil McGregor

6/15/2014 1:55:00 AM

0

On Fri, 13 Jun 2014 19:34:09 -0500, Rich Rostrom
<rrostrom.21stcentury@rcn.com> wrote:

>Extremely concerned. They have no way to
>judge the effectiveness of Soviet arms; the
>Soviets spanked the Japanese in Manchuria,
>so they don't look bad.

But did the Finns (or anyone else, much) actually *know* the *details*
.... or anything much ... about what went on at Nomonhan?

It wasn't widely reported, was certainly not reported in detail, and
is still, today, not widely studied and not widely understood ... by
the general public at the very least.

I agree the Finns would be worried, more by the size of the David vs.
Goliath match they'd see themselves stuck in, though.

Phil

Phil McGregor

6/15/2014 2:00:00 AM

0

On Sat, 14 Jun 2014 15:49:25 -0700, The Horny Goat <lcraver@home.ca>
wrote:


>Hitler was expecting to win in Russia in 1941 and was unprepared for a
>winter campaign -

Hitler knew that he couldn't successfully *maintain* a long campaign.

All the planning he'd had done for all the campaigns to that date had
run into the same problem ... the German economy couldn't maintain a
long term campaign without a lot more resources and time than they
would have to prepare for such.

Hence, he had to win all of them in as short a period as possible.

The planning didn't always assume as short a period as was actually
achieved (as with France and the Low Countries), but it was always
assumed that they would have to achieve a short war.

It is more correct to say that Hitler wasn't prepared for any sort of
*extended* campaign, not just a 'winter' campaign.

Phil