English 中文(简体)
利用Salb的Sql服务器数据库检索数据
原标题:Retrieve data from sql server database using Python

我正试图执行以下文字,但我没有获得预期结果,也没有错误信息。

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,))

我如何能够从AQ服务器数据库获得数据?

最佳回答

你们必须与治疗师一道使用手法。 例

for row in cursor.fetchall():
    print( row = %r  % (row,))

http://www.un.org。

独角兽部队在名单上的所有剩余行人都返回。

    If there are no rows, an empty list is returned. 
    If there are a lot of rows, *this will use 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 :

for row in cursor.execute("select bla, anotherbla from blabla"): 
    print row.bla, row.anotherbla

https://code.google.com/archive/p/pyodbc/wikis/GettingStarted.wiki” rel=“nofollow noreferer”>Documentation

问题回答

I found this information useful to retrieve data from SQL database to python as a data frame.

import pandas as pd
import pymssql

con = pymssql.connect(server= use-et-aiml-cloudforte-aiops- db.database.windows.net ,user= login_username ,password= login_password ,database= database_name )
cursor = con.cursor()

query = "SELECT * FROM <TABLE_NAME>"
cursor.execute(query)
df = pd.read_sql(query, con)
con.close()

df

1. 进口我sql. 连接器作为立方米

connection creation

conn = mc.connect(host= localhost , user= root , passwd= password ) print(conn)

cur = conn.cursor() print(cur)

曲线(显示数据库)

for i in cur: print(i)

query = "Select * from employee_performance.employ_mod_recent" emp_data = pd.read_sql(query, conn) emp_data





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签