<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Development Blog &#187; MSSQL</title>
	<atom:link href="http://blog.yeticode.co.uk/tag/mssql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.yeticode.co.uk</link>
	<description>John Tindell's Blog For Development Related Things</description>
	<lastBuildDate>Wed, 23 Jun 2010 18:17:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Python and MSSQL</title>
		<link>http://blog.yeticode.co.uk/2009/06/python-and-mssql/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=python-and-mssql</link>
		<comments>http://blog.yeticode.co.uk/2009/06/python-and-mssql/#comments</comments>
		<pubDate>Sat, 20 Jun 2009 18:03:53 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Development Log]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[MSSQL]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://blog.yeticode.co.uk/?p=246</guid>
		<description><![CDATA[I have been playing about with django a fair bit over the last week. After written a small app with django I wanted to import some data from an existing MSSQL database. To get the data into the django model I created a seperate python script to perform the import. Below is a script to [...]]]></description>
			<content:encoded><![CDATA[<p>I have been playing about with django a fair bit over the last week. After written a small app with django I wanted to import some data from an existing MSSQL database. To get the data into the django model I created a seperate python script to perform the import. Below is a script to import data from a table compaines in a mssql data to a django model called Company. To connect to the mssql server I used the <a href="http://pymssql.sourceforge.net">pymssql</a> library.</p>
<pre name="code" class="python">
#!/usr/bin/env python
import sys,os
os.environ['DJANGO_SETTINGS_MODULE'] ='settings'
# import the modules needed to start the import
import pymssql
from datetime import *
import unicodedata
from django.core.management import setup_environ
import settings

setup_environ(settings)

#import the django models
from apps.clients.models import *

def importCompanies(conn):
	cur = conn.cursor()
	cur.execute("""SELECT
			liCompanyPk,
			szCompanyName
		FROM companies""")
	row = cur.fetchone()
	while row:
		company = Company()
		company.pk = row[0]
		company.Name = row[1]
		company.save()
		row = cur.fetchone()

if len(sys.argv) != 5:
	print 'data_import.py [server] [username] [password] [database]'
else:
	print 'host=%s, user=%s, password=%s, database=%s' % (sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4])
	conn = pymssql.connect(host=sys.argv[1], user=sys.argv[2], password=sys.argv[3], database=sys.argv[4])
	importCompanies(conn)
	conn.close()
</pre>
<p>After this I started to play around with pymssql I wanted a quick way to execute simple sql queries, not knowing of an linux alternative to the mysql console client I write another simple script to provide this basic functionality.</p>
<pre name="code" class="python">
#!/usr/bin/env python
import sys,os
import _mssql

if len(sys.argv) != 5:
	print 'data_import.py [server] [username] [password] [database]'
else:
	print 'host=%s, user=%s, password=%s, database=%s' % (sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4])
	conn = _mssql.connect(server=sys.argv[1], user=sys.argv[2], password=sys.argv[3], database=sys.argv[4])
	print '>',
	cmd = raw_input()
	while cmd != 'exit':
		if cmd != '':
			print cmd
			try:
				conn.execute_query(cmd)
				for row in conn:
					for i in range(0,( len(row) / 2 )):
						print row[i],'|',
					print ''
			except _mssql.MssqlDatabaseException,e:
				print e
		print '>',
		cmd = raw_input();

	conn.close()
</pre>
<p><a href="http://dev.yeticode.co.uk/python/mssql_client.py">You can download mssql_client.py here</a></p>
<p>So after only using for python for a couple of weeks It&#8217;s definitely powerful and fun to code with. Now to try out for all of the libraries that provide bindings such as pysvn or pyldap.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.yeticode.co.uk/2009/06/python-and-mssql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
