Python sql 服务器使用 pyodbc

Python with sql server using pyodbc

使用 pyodbc 从 sql 查询中获取记录后,我如何访问特定记录并将其存储在变量中。

P.S。我是新手 python.

你可以这样使用:

import pyodbc

conn = pyodbc.connect('DSN=SQLS;UID=test01;PWD=test01')
cursor=conn.cursor()
cursor.execute("create table rvtest (col1 int, col2 float, col3 varchar(10))")
cursor.execute("insert into rvtest values(1, 10.0, \"ABC\")")
cursor.execute("select * from rvtest")

   records =cursor.fetchall()
    for row in records:
        print("Id: ", row[0])
        print("Name: ", row[1])`

如果要获取第 2 行第 5 列,请使用:

records[1][4]