3

Problem

I am trying to use python to read my csv file and put it into Microsoft SQL Server 2016 as a new table. Simply put, I don't want to create a table on SQL and import the csv, I want to write a script in python that can read the csv and create a new table in SQL for me.

UPDATE

I may have to rethink my approach. I corrected the driver, but I am getting the following error from to_sql. I am thinking that there is something wrong with my authentication scheme. Sadly, the to_sql documentation and sql_alchemy is not shedding much light. Starting to consider alternatives.

sqlalchemy.exc.DBAPIError was unhandled by user code
Message: (pyodbc.Error) ('08001', '[08001] [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53].  (53) (SQLDriverConnect)')

Code

import pandas as pd 
import sqlalchemy

#Read the file 
data = pd.read_csv(file.csv)

#Connect to database and write the table 
 server = "DERPSERVER"
 database = "HERPDB"
 username = "DBUser" 
 password = "password"
 tablename = "HerpDerpTable"
 driver = "SQL+Server+Native+Client+11.0" 

 #Connect to SQL using SQL Server Driver 
 print("Connect to SQL Server")
 cnxn = sqlalchemy.create_engine("mssql+pyodbc://"+username+":"+password+"@"+server +"/"+database + "?driver="+driver)

UPDATE

I rewrote the string as follows, but it doesn't work:

sqlalchemy.create_engine('mssql+pymssql://'+username+':'+ password + '@' + server + '/' + database + '/?charset=utf8')


data.to_sql(tablename, cnxn);

Attempts

These are some important things to note in my approach. Pay special attention to the second bullet point I share below. I think my connection string for create_engine is somehow or maybe wrong, but don't know what is wrong because I followed the documentation.

  • I believe I am in a DSN-less situation. Thus, was attempting to connect by other means as described by the documentation.
  • I was using this link to help me create the connection string part in create_engine.
  • I tried to_sql to write the to the database, but think my connection string might still be messed up? I consulted this question on stackoverflow.
  • Update I added the driver specification as MaxU and the documentation for sqlalchemy specified. However, I am getting an error saying my data source name was not found and no default driver is specified with to_sql. Do I need to feed to_sql the driver as well? If so, where is the documentation or a sample code that shows me where I am going wrong?

I am making good effort to pick up python and to use it as a scripting language because of future goals and needs. I would appreciate any assistance, help, mentorship rendered.

hlyates
  • 1,279
  • 3
  • 22
  • 44

1 Answers1

6

You should explicitly specify the MS SQL Server driver if you use SQLAlchemy version 1.0.0+:

Example:

engine = create_engine("mssql+pyodbc://scott:tiger@myhost:port/databasename?driver=SQL+Server+Native+Client+10.0")

Changed in version 1.0.0: Hostname-based PyODBC connections now require the SQL Server driver name specified explicitly. SQLAlchemy cannot choose an optimal default here as it varies based on platform and installed drivers.

List of Native SQL Server Client Names

  • SQL Server 2005:

SQL Server Native Client

  • SQL Server 2008:

SQL Server Native Client 10.0

  • SQL Server 2016:

SQL Server Native Client 11.0


Alternatively you can use pymssql:

engine = create_engine('mssql+pymssql://<username>:<password>@<freetds_name>/?charset=utf8')
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
  • I read the link, thank you. If possible, could you please be so kind and gracious enough to answer the following question? How do I know what SQL Server native driver name to use? Thanks! – hlyates Jan 19 '17 at 02:35
  • I updated my question. I think we are almost there, but ran into a driver error with `to_sql`, but `sqlalchemy` and `pyodbc` are both complaining, so not sure what's going on there. Do I need to feed `to_sql` a driver parameter? – hlyates Jan 19 '17 at 14:36
  • 1
    @hlyates, you may want to try [`SQL Server Native Client 11.0` for SQL Server 2016](https://msdn.microsoft.com/en-us/library/ms130822.aspx) – MaxU - stand with Ukraine Jan 19 '17 at 15:29
  • Definitely have done so, updated with my current issue. When I fight a tool too much, I start to think about alternative approach. I want a standard way of writing python scripts to take my csv data and create a table in a SQL database. Can you offer suggestions please? – hlyates Jan 21 '17 at 20:07
  • Okay, I will give that a try sir. If pymssql works, but to_sql fails, I'll just try pyodbc with manual sql code specified. I'm hoping to skip the insert sql statements with pymssql + to_sql approach. Does this sound reasonable to you? I'll be sure to mark yours as an answer soon regardless. – hlyates Jan 21 '17 at 20:16
  • 1
    @hlyates, `df.to_sql('table_name', sqlalchemy_engine_object)` - should work, as soon as `sqlalchemy_engine_object` is a valid SQLAlchemy engine object – MaxU - stand with Ukraine Jan 21 '17 at 20:21
  • Sure, I believe you. The thing is, the only thing throwing me off in the sqlalchemy docs is freetds_name. How does this connection stream know what the server and database is called? – hlyates Jan 21 '17 at 20:40
  • I rewrote the string, but getting a unknown database error. I'm still trying, but about ready to junk the entire approach and brute force this. :-( – hlyates Jan 21 '17 at 20:53
  • I couldn't get it to work, but as promised I'll mark yours as answer as I think in general it works, but was unable to get either string to properly work. I am following this to help me and having success: https://learn.microsoft.com/en-us/azure/sql-database/sql-database-develop-python-simple – hlyates Jan 21 '17 at 23:43
  • @hlyates, so, was it a wrong ODBC driver (or ODBC DSN)? – MaxU - stand with Ukraine Jan 22 '17 at 00:06
  • 1
    It doesn't have a DSN, but I think it has to do something with sql authentication process. The errors were terrible, completely generic and basically "something went wrong". Leaving me to guess. As the link indicates, MS forces a user to go the long route with the connection string and commands in my belief. Using pyodbc in the link reveals that the ODBC Driver 13 for SQL Server might be the magic. I really feel the above approach is preferable and that I got close, but believe I can't get to the bottom of it with the generic errors I was getting. :-( – hlyates Jan 22 '17 at 00:31
  • 1
    @hlyates, thanks for detailed explanation - it might help others, who might have the same problem – MaxU - stand with Ukraine Jan 22 '17 at 00:33