수색…


pyodbc 사용

import pandas.io.sql
import pyodbc
import pandas as pd

매개 변수 지정

# Parameters
server = 'server_name'
db = 'database_name'
UID = 'user_id'

연결 만들기

# Create the connection
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db + '; UID = ' + UID + '; PWD = ' + UID + 'Trusted_Connection=yes')

팬더 데이터 프레임에 대한 질의

# Query into dataframe
df= pandas.io.sql.read_sql('sql_query_string', conn)

연결 루프와 함께 pyodbc 사용하기

import os, time
import pyodbc
import pandas.io.sql as pdsql

def todf(dsn='yourdsn', uid=None, pwd=None, query=None, params=None):
    ''' if `query` is not an actual query but rather a path to a text file 
        containing a query, read it in instead '''
    if query.endswith('.sql') and os.path.exists(query):
        with open(query,'r') as fin:
            query = fin.read()
        
    connstr = "DSN={};UID={};PWD={}".format(dsn,uid,pwd)
    connected = False
    while not connected:
        try:
            with pyodbc.connect(connstr,autocommit=True) as con:
                cur = con.cursor()
                if params is not None: df = pdsql.read_sql(query, con, 
                                                           params=params)
                else: df = pdsql.read_sql(query, con)
                cur.close()
            break 
        except pyodbc.OperationalError:
            time.sleep(60) # one minute could be changed
    return df


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow