tags:

views:

133

answers:

1

When I first started working at the company that i work at now, I created a java application that would run batches of jasper-reports. In order to determine which parameters to use for each report in the set of reports, I run a sql query (on sqlserver). I wrote the application to take an xml file with a set of parameters for each report to be generated in the set. so, my process has become, effectively, three steps:

  1. run the sql query and return the results in XML format (using 'for XML auto')
  2. run the results of the sql query through an XSLT transformation so the xml is formatted in such a way that is friendly with the java application i wrote.
  3. run the java application with that final xml file

As you can imagine, what I'd like to do is accomplish these steps in python, but i'm not quite sure how to get started. I know how to run an SQL query in Python. I see plenty of documentation about how to write your own xml document with Python. I even see documentation for xsl transformations in python.

the big question is how to get the results of the sql query in XML through python. Any and all pointers would be very valuable. Thanks, _Ramy

A: 

I would imagine that executing the sql query you have using 'FOR XML AUTO' will give you a recordset with one record in it (the xml). You would then retrieve the first record and continue with your application from there.

Example using pyodbc:

cursor.execute("select user_name from users where user_id=? for xml auto", userid)
xml = cursor.fetchone()
if xml:
    # do your xsl transformation and other processing here...

Edit

I selected a lot of fields in my test and (I think) managed to exceed some buffer size for the row objects, so it split the record.

Based on my test the code I would need would be:

cursor.execute("select user_name from users where user_id=? for xml auto", userid)
rows = cursor.fetchall()
xml = ''.join(row[0] for row in rows)
tgray
very close actually. the problem is the one record it returns is a LINK to the XML document (as opposed to a field with XML text in it)...
Ramy
where does the link point to? a local folder?
tgray
when I did a test with it, I got a recordset with two records, but they contained XML (which needed to be joined together before it was valid), not a link...
tgray
it points to a temporary location in memory. if I left click the link it opens a new editor tab and displays the xml. if I right click, i can choose "save as".I'm using SQLServer 2008.I'll try to call the query from python with the for xml auto line at the bottom.
Ramy
well this is taking a different turn. I'm getting this exception:error: SQL Server message 4004, severity 16, state 1, line 16:Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier.DB-Lib error message 10007, severity 5:General SQL Server error: Check messages from the SQL Server.
Ramy
i've seen a few different posts in different places about this exception. The closest thing to a solution i've found is here: http://use.perl.org/~jdavidb/journal/37102except that I can't seem to find any freetds.conf file....
Ramy
That looks like a pymssql issue with ntext columns. Check out their FAQ: http://code.google.com/p/pymssql/wiki/FAQ . To paraphrase, CAST or CONVERT any NTEXT columns to NVARCHAR.
tgray