简体   繁体   中英

Python with sql server using pyodbc

After fetching records from sql query using pyodbc, how do i access particular record and store it in a variable.

PS I am novice to python.

You can use something like this:

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])`

If you want to fetch 2nd row 5th column use:

records[1][4]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM