简体   繁体   中英

Pulling data from SQL Server using Dask pyodbc, and SQLAlchemy

I want to use Dask to pull a bunch of data.

I am using

SQLAlchemy==1.3.9
dask==2.5.2
pyodbc==4.0.27

I have the following files in the same directory as the scrips:

odbc.ini
odbcinst.ini

and I can't get the configuration right. I have seen a bunch of connection strings for SQLAlchemy, but I have not been able to get any one of them to work.

This code works:

#!/usr/bin/env python
import pyodbc
import getpass

#odbc is the prefered method for contacting microsoft sqlserver
sql_server_cnxn_str = 'DRIVER={ODBC Driver 17 for SQL Server};' + \
                      'SERVER=XXXX;DATABASE=YYYY;' + \
                      'Trusted_Connection=yes;'
#get username
print ("Input username then hit enter: ")
name = input()
#Get password
psswd = getpass.getpass()
#Create connection using odbc
conn = pyodbc.connect(sql_server_cnxn_str)
cursor = conn.cursor()

query = "SELECT * from Foo.Testing"

cursor.execute(query)
data = cursor.fetchall()
print(data)

So I know the connection works.

This is the code I am trying to get to work:

#!/usr/bin/env python
import pyodbc
import getpass
import dask.dataframe as dd
from dask.diagnostics import ProgressBar

#odbc is the prefered method for contacting microsoft sqlserver
sql_server_cnxn_str = 'DRIVER={ODBC Driver 17 for SQL Server};' + \
                      'SERVER=XXXX;DATABASE=YYYY;' + \
                      'Trusted_Connection=yes;'
#get username
print ("Input username then hit enter: ")
name = input()
#Get password
psswd = getpass.getpass()

#If neither divisions or npartitions is given, the memory footprint of the first few rows will be determined, and partitions of size ~256MB will be used.
data = dd.read_sql_table("Foo.Testing", sql_server_cnxn_str, index_col="Test")

I keep getting this error:

File "src/pymssql.pyx", line 642, in pymssql.connect
sqlalchemy.exc.OperationalError: (pymssql.OperationalError) (18456, b"Login failed for user 'foobar'.DB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (ROEFDN819Q)\nDB-Lib error message 20002, severity 9:\nAdaptive Server connection failed (ROEFDN819Q)\n")

I have tried many connection string formats including: 'mssql+pyodbc://server_name/database_name?driver=SQL Server?Trusted_Connection=yes'

"mssql+pymssql://{user}:{password}@{host}:{port}/{database}".format(user=username, password=password, host=server, database=database, port=port)

("mssql+pyodbc://%s:%s@%s/%s?driver=%s" % (username, password, server, database, driver ) )

"mssql+pyodbc://{0}:{1}@XXXX/YYYY".format(username,password)

Nothing works. Can't I just pull the correct configuration from pyodbc somehow? Thanks so much for your help!

How about this?

import pypyodbc 
cnxn = pypyodbc.connect("Driver={SQL Server Native Client 11.0};"
                        "Server=Server_Name;"
                        "Database=DB_Name;"
                        "Trusted_Connection=yes;")

cursor = cnxn.cursor()
cursor.execute('SELECT * FROM Actions')

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

Or, with login creds, try this?

pyodbc.connect("Driver = {SQL Server Native Client 11.0};"               
               "Server = Server_Name;"
               "Database = Database_Name;"
               "username = User_Name;"
               "password = User_Password;"
               "Trusted_Connection = yes;")

I have never used Dask before, but this looks promising.

username = 'jesse'
password = 'DataScienceRulez'
hostname = 'localhost'
database_name = 'DSAS'
odbc_driver = 'ODBC+Driver+13+for+SQL+Server'

connection_string = 'mssql+pyodbc://{0}:{1}@{2}/{3}?driver={4}'.format(username, password, hostname, database_name, odbc_driver)

data = dd.read_sql_table('violations', connection_string, index_col='Summons Number')

Resource:

https://livebook.manning.com/book/data-science-at-scale-with-python-and-dask/chapter-4/72

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