Tagged: SQL RSS Toggle Comment Threads | Keyboard Shortcuts

  • John 7:03 pm on June 20, 2009 Permalink | Reply
    Tags: , MSSQL, , SQL   

    Python and MSSQL 

    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 pymssql library.

    #!/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()
    

    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.

    #!/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()
    

    You can download mssql_client.py here

    So after only using for python for a couple of weeks It’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.

     
  • John 6:44 pm on February 22, 2009 Permalink | Reply
    Tags: Service Broker, SQL   

    SQL Service Broker 

    After trying to receive a message from a service broker queue for a while I found that it will not throw an error if your database isn’t setup correctly. After looking in the table where the messages are stored before they are sent to the queue the solution was easily found

    The Error

    An exception occurred while enqueueing a message in the target queue. Error: 15517, State: 1. Cannot execute as the database principal because the principal "dbo" does not exist, this type of principal cannot be impersonated, or you do not have permission.

    Where messages are stored before they are sent to queue, and any exceptions which occurred

    select * from sys.transmission_queue
    

    Fix

    USE [database_name]
    ALTER DATABASE [database_name]SET TRUSTWORTHY ON
    ALTER DATABASE [database_name] SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
    ALTER AUTHORIZATION ON DATABASE::[database_name] TO [SA];
    

    Developer.com provides a good simple introduction to Service Broker
    http://www.developer.com/db/article.php/3640771

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel