I am trying to execute the following script. but I don't get neither the desired results nor a error message ,and I can't figure out where I'm doing wrong.
import pyodbc
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=mySRVERNAME;"
"Database=MYDB;"
"uid=sa;pwd=MYPWD;"
"Trusted_Connection=yes;")
cursor = cnxn.cursor()
cursor.execute('select DISTINCT firstname,lastname,coalesce(middlename,\' \') as middlename from Person.Person')
for row in cursor:
print('row = %r' % (row,))
any ideas ? any help is appreciated :)
3条答案
按热度按时间ne5o7dgx1#
You have to use a fetch method along with cursor. For Example
EDIT :
The fetchall function returns all remaining rows in a list.
Unread rows are stored by the database driver in a compact format and are often sent in batches from the database server.
Reading in only the rows you need at one time will save a lot of memory.
If we are going to process the rows one at a time, we can use the cursor itself as an interator Moreover we can simplify it since cursor.execute() always returns a cursor :
Documentation
nkkqxpd92#
I found this information useful to retrieve data from SQL database to python as a data frame.
w6lpcovy3#
import mysql.connector as mc
connection creation
conn = mc.connect(host='localhost', user='root', passwd='password') print(conn)
#create cursor object
cur = conn.cursor() print(cur)
cur.execute('show databases')
for i in cur: print(i)
query = "Select * from employee_performance.employ_mod_recent" emp_data = pd.read_sql(query, conn) emp_data