473,396 Members | 2,154 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,396 software developers and data experts.

pymssql query

Hi,

I'm trying to use pymssql to execute a stored procedure. Currently, I
have an Excel spreadsheet that uses VBA in this manner:

Private Function CreateNewParrot(connDb As ADODB.Connection) As Long
Dim objCommand As ADODB.Command
Dim iParrot As Long
Dim bSuccess As Boolean

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = connDb
objCommand.CommandText = "create_new"
objCommand.CommandType = adCmdStoredProc
objCommand.Parameters.Refresh
On Error Resume Next
Err.Clear
objCommand.Execute
bSuccess = (Err.Number = 0)
On Error GoTo 0
If (bSuccess) Then
If (IsNull(objCommand("@parrot"))) Then
iParrot = 0
Else
iParrot = CLng(objCommand("@parrot"))
End If
Else
iParrot = 0
End If
Set objCommand = Nothing
CreateNewParrot = iParrot
End Function

My attempts to translate this into a python script using pymssql have
so far been fruitless. Here is what I have tried:
>>import pymssql
con = pymssql.connect(host='blah', user='blah', password='blah', database='blah')
cur = con.cursor()
command = 'exec create_new_parrot'
cur.execute(command, '@parrot')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Program Files\Python\lib\site-packages\pymssql.py", line
127, in execute
self.executemany(operation, (params,))
File "C:\Program Files\Python\lib\site-packages\pymssql.py", line
153, in executemany
raise DatabaseError, "internal error: %s" % self.__source.errmsg()
pymssql.DatabaseError: internal error: SQL Server message 8114,
severity 16, state 5, procedure create_new_parrot, line 0:
Error converting data type nvarchar to int.
DB-Lib error message 10007, severity 5:
General SQL Server error: Check messages from the SQL Server.

Any ideas?

Thanks.

<M>

May 16 '07 #1
1 6674
m.***********@gmail.com wrote:
Hi,

I'm trying to use pymssql to execute a stored procedure. Currently, I
have an Excel spreadsheet that uses VBA in this manner:

Private Function CreateNewParrot(connDb As ADODB.Connection) As Long
Dim objCommand As ADODB.Command
Dim iParrot As Long
Dim bSuccess As Boolean

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = connDb
objCommand.CommandText = "create_new"
objCommand.CommandType = adCmdStoredProc
objCommand.Parameters.Refresh
On Error Resume Next
Err.Clear
objCommand.Execute
bSuccess = (Err.Number = 0)
On Error GoTo 0
If (bSuccess) Then
If (IsNull(objCommand("@parrot"))) Then
iParrot = 0
Else
iParrot = CLng(objCommand("@parrot"))
End If
Else
iParrot = 0
End If
Set objCommand = Nothing
CreateNewParrot = iParrot
End Function
Depending on what you're after, why not transliterate
it into Python?

import win32com.client
command = win32com.client.Dispatch ("ADODB.Command")

etc.
My attempts to translate this into a python script using pymssql have
so far been fruitless. Here is what I have tried:
>>>import pymssql
con = pymssql.connect(host='blah', user='blah', password='blah', database='blah')
cur = con.cursor()
command = 'exec create_new_parrot'
cur.execute(command, '@parrot')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\Program Files\Python\lib\site-packages\pymssql.py", line
127, in execute
self.executemany(operation, (params,))
File "C:\Program Files\Python\lib\site-packages\pymssql.py", line
153, in executemany
raise DatabaseError, "internal error: %s" % self.__source.errmsg()
pymssql.DatabaseError: internal error: SQL Server message 8114,
severity 16, state 5, procedure create_new_parrot, line 0:
Error converting data type nvarchar to int.
DB-Lib error message 10007, severity 5:
General SQL Server error: Check messages from the SQL Server.
Well, I'm not connected to a SQL Server here (so this
is untested) but I don't believe pymssql handles
stored procedure calls differently from any other
SQL. Which is a problem here because, as far as I
can make out from your code above, @parrot is an
output parameter for the create_new stored proc.
Is that right? If it's an input-only param, then
just do the usual:

import pymssql
db = pymssql.connect (...)
q = db.cursor ()
q.execute (
"EXECUTE create_new @parrot = %s",
["parrot-thing"]
)

I'm not aware of any of the MSSQL dbapi
modules which allow for output parameters
in stored procedures. pyodbc (one of the most
recent entrants) tantalisingly offers a .callproc
but then comments "not yet supported". If the ADO
approach works, I'd use that if I were you!

TJG
May 16 '07 #2

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

Similar topics

0
by: Khawaja Shahzad Sadiq Butt | last post by:
Hi, I am trying to connect to sql server 2000 on win xp. I am using a python extension called. pymssql but it is giving me this errror: Traceback (most recent call last): File...
0
by: Josh Close | last post by:
I'm getting an error from pymssql when trying to execute a stored procedure: Operating-system Error - OpenClient I can't figure out why this is happening. It's probably a freetds problem (like...
0
by: saqib | last post by:
I have a query that is running on a mssql DB that takes about 15 minutes when run "directly"; IE: thru MS SQL Query Analyzer. I know, it sucks. I'm trying to fix it. But in the meantime, I have...
1
by: morris carre | last post by:
I have a strange problem : some code that fetches queries from an mssql database works fine under Idle but the very same code run from a shell window obtains its strings garbled as if the encoding...
1
by: eight02645999 | last post by:
hi i am using pymmsql to query a date column in MSSQL database table. However the result shows for example (datetime.datetime(2006, 2, 16, 17, 50, 19) which is supposed to be 2006-02-16...
3
by: rc | last post by:
How to insert NULL values in to int field using params. I'm trying to use pymssql.execute, passing the operation and list of params. One of the values in the params is a NULL value going to int...
1
by: James Su | last post by:
I tried to use pymssql to access MSSQL 2000, with a table, I store Chinese Character in NVarchar field, Chinese Character display normally when I query them by MS SQL Query Analyzer under Windows...
7
by: ChaosKCW | last post by:
Hi I am trying to use pymssql, and have an issue where by the execute (not the fetch) is appearing to load all records into memory. if I execute con = pymssql.connect(...) cur =...
0
by: Aspersieman | last post by:
On Tue, 21 Oct 2008 20:44:24 +0200, Mike Hansen <projecktzero@yahoo.com> wrote: Hi there I hope you don't mind - I made this a new thread on the list, so others can maybe help out. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.