×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Python
Posted by: Alberto Amerio
Added: Sep 15, 2011 2:45 PM
Views: 126
Tags: no tags
Small sample to access SQL Server database by ODBC and SqlAlchemy
  1. #!/usr/bin/python
  2. # access to SQL Server database
  3. import pyodbc
  4.  
  5. #by odbc
  6. cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=pubs;UID=sa;PWD=ogilvyone03')
  7. cursor = cnxn.cursor()
  8.  
  9. cursor.execute("select job_id, job_desc from jobs")
  10. rows = cursor.fetchall()
  11. for row in rows:
  12.     print row
  13.  
  14. print ' ---- fine test ---- '
  15.  
  16. #by  orm (sqlalchemy)
  17. import sqlalchemy as sa
  18. eng = sa.create_engine("mssql://sa:ogilvyone03@/?dsn=pubs", echo=False)
  19. res = eng.execute('select count(*) foo from jobs')
  20. for row in res:
  21.     print 'jobs count=', row['foo']
  22.