Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm attempting to recover information from a SQL server utilizing pyodbc and print it in a table utilizing Python. In any case, I can just appear to recover the column name and the information type and stuff that way, not the real information values in each line of the column.

Essentially, I am attempting to imitate an Excel sheet that recovers server information and showcases it in a table. I'm not experiencing any difficulty interfacing with the server, simply that I can't locate the real information that goes into the table.

Look at the code:

import pyodbc

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=SQLSRV01;DATABASE=DATABASE;UID=USER;PWD=PASSWORD')

cursor = cnxn.cursor()

cursor.execute("SELECT * FROM sys.tables")

tables = cursor.fetchall()

#cursor.execute("SELECT WORK_ORDER.TYPE,WORK_ORDER.STATUS, WORK_ORDER.BASE_ID, WORK_ORDER.LOT_ID FROM WORK_ORDER")

for row in cursor.columns(table='WORK_ORDER'):

    print row.column_name

    for field in row:

        print field

Anyway, the result of this simply gives me things like the table name, the segment names, and a few numbers and 'None's and things like that that aren't important to me: 

STATUS_EFF_DATE

DATABASE

dbo

WORK_ORDER

STATUS_EFF_DATE

93

datetime

23

16

3

None

0

None

None

9

3

None

80

NO

61

So I'm not quite certain where I can get the values to top off my table. Would it be in table='WORK_ORDER', yet could it be under an alternate table name? Is there a method of printing the information that I am simply absent?

Anyone, please help me.

1 Answer

0 votes
by (26.4k points)

Actually, You came near.

import pyodbc

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=SQLSRV01;DATABASE=DATABASE;UID=USER;PWD=PASSWORD')

cursor = cnxn.cursor()

cursor.execute("SELECT WORK_ORDER.TYPE,WORK_ORDER.STATUS, WORK_ORDER.BASE_ID, WORK_ORDER.LOT_ID FROM WORK_ORDER")

for row in cursor.fetchall():

    print row

Interested to learn python in detail? Come and Join the python course.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...