from sqlalchemy import create_engine
engine = create_engine('mssql+pymssql://myusername:mypassword@127.0.0.1:1433/AQOA_Core')
connection = engine.connect()
result = connection.execute("""SELECT DISTINCT Distributor FROM Product""")
for row in result:
print row[0]
connection.close()
上面的代码返回一个结果集:
FRANCETV
GAUMONT
M6SND
PATHE
STUDIOCANAL
TF1
WARNER
如果我想在不更改查询的情况下只打印一个值,该怎么办?
尝试print row[0][1]
,print row[0:1]
尝试仅打印第一个值FRANCETV
基本上我希望能够单独打印结果集中的每个值,而无需更改查询。
答案 0 :(得分:3)
您可以尝试通过以下方式访问数据:
connection = engine.connect()
result = connection.execute("""SELECT DISTINCT Distributor FROM Product""")
result_list = result.fetchall()
result_list[0][0]
connection.close()
答案 1 :(得分:1)
如果你看一下pymssql文档,你会发现游标对象有一个fetchone()
方法。
with engine.connect() as connection:
cursor = connection.cursor()
cursor.execute("""SELECT DISTINCT Distributor FROM Product""")
first_row = cursor.fetchone()
print first_row