SQLAlchemy学习笔记

from sqlalchemy import *

engine=create_engine("mssql+pymssql://sa:yhdtpass@localhost:1433/bwbook",echo=True)

with engine.connect() as conn:

rs=conn.execute('select top 10 * from db_product')

# data=rs.fetchall()

for a in rs:

print(a)

1.sqlalchrem 与python连接需要 导入第三方包pymssql包

2.直接用session.execute('SQL语句')可以直接查询但是用conn=engine.connect() text()方法

engine=create_engine("mssql+pymssql://sa:yhdtpass@localhost:1433/bwbook",echo=True)

conn=engine.connect()

text_sql="SELECT TOP 10 * FROM dbo.db_product where h_id=:isbn"

s=text(text_sql)

k=conn.execute(s,isbn='0').fetchall()

print(k)

你可能感兴趣的:(SQLAlchemy学习笔记)