# -*- coding: ISO-8859-1 -*-
#test.py
import sqlalchemy import traceback import pyodbc from sqlalchemy import create_engine from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey from sqlalchemy.orm import mapper from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import deferred
class User(object): def __init__(self, name, usercode, password,id): self.name = name self.usercosde = usercode self.password = password self.id = id def __repr__(self): return "
print sqlalchemy.__version__ try:
engine = create_engine('mssql://sa:123456@/?dsn=officialcar',module=pyodbc,echo=True)
metadata = MetaData() users_table = Table('t_user_info', metadata, Column('id', Integer, primary_key=True), Column('name', String(40)), Column('usercode', String(100)), Column('password', String(15)) ) #metadata.create_all(engine)
#mapper(User, users_table,properties = {'name' : deferred(users_table.c.name)}) mapper(User, users_table,order_by=users_table.c.name.desc())
#ed_user = User(unicode('测试'), '8888', 'edspassword') #ed_user = User(None,'测试', '8888', 'edspassword') #print ed_user.name #print ed_user.usercode
Session = sessionmaker(bind=engine, autoflush=True, transactional=True)
session = Session() #session.save(ed_user)
#session.commit() #print ed_user.id
users = session.query(User).filter(User.name.like('陈%')) #users = session.query(User) #print 'size',len(users) for user in users: print 'new',repr(user)
#conn = engine.connect() #result = conn.execute("select name from t_user_info") #for row in result: # print "username:", row['name'] #conn.close()
#cnxn = pyodbc.connect("DSN=officialcar;UID=sa;PWD=123456") #cursor = cnxn.cursor() #cursor.execute('select * from t_user_info') #a=cursor.fetchall() #print 'pyodbc',a except : traceback.print_exc()