Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymssql
- server='dbhost'
- user ='myuser'
- password='mypass!'
- database_name= 'mydb'
- class Row(dict):
- """A dict that allows for object-like property access syntax."""
- def __getattr__(self, name):
- try:
- return self[name]
- except KeyError:
- raise AttributeError(name)
- def query(sql):
- try:
- con = pymssql.connect(server, user, password, database_name)
- cur = con.cursor()
- cur.execute(sql)
- column_names = [d[0] for d in cur.description]
- return [Row(zip(column_names, row)) for row in cur]
- con.close()
- except Exception, e:
- print '%s' % (e)
- def get(sql):
- rows = query(sql)
- if not rows:
- return None
- elif len(rows) > 1:
- raise Exception("Multiple rows returned for Database.get() query")
- else:
- return rows[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement