I've been at this for many hours, and cannot figure out what's wrong with my approach. I'm trying to read a table into pandas using sqlalchemy (from a SQL server 2012 instance) and getting the following error:
DBAPIError: (Error) ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'SQL Server' : file not found (0) (SQLDriverConnect)") None None
I'm using the following code:
import sqlalchemy as sql
from sqlalchemy import create_engine
import pyodbc
pyodbc.connect('DSN=MYDSN;UID=User;PWD=Password')
Which returns:
<pyodbc.Connection at 0x10a26a420>
Which I think is good. Then when I run the following:
connectionString = 'mssql+pyodbc://User:Password#IPAdress/Database'
engine = sql.create_engine(connectionString)
pd.read_sql("ecodata", engine)
I get the following error mentioned above.
Is there something wrong with my driver setup? I've been wrestling with the driver setup for days and thought I had it beat.
Any help is greatly appreciated.
For the record, the answer to my question was figured out by joris:
My syntax for the connection string was wrong, and when I changed it from this
connectionString = 'mssql+pyodbc://User:Password#IPAdress/Database'
to this
connectionString = 'mssql+pyodbc://User:Password#IPAddress:Port/Database?driver=FreeTDS'
It worked!
Thanks again for the help!
Try forming your connection like this. You need a few more parameters.
con = pyodbc.connect('DRIVER={FreeTDS};SERVER="yourserver";PORT=1433;DATABASE=yourdb;UID=youruser;PWD=yourpassword;TDS_Version=7.2;')
To figure out which TDS Version to use:
http://www.freetds.org/userguide/choosingtdsprotocol.htm
Hopefully, this helps!
Related
I'm trying to connect to Teradata without using Teradata driver. so installed the teradatasql package and written the below code to connect.
import teradatasql
import pandas as pd
with teradatasql.connect(host='abc.abc.net', user='abcabc', password='abce123') as connect;
query = "select * from abc.emp;"
df = pd.read_sql(query, connect)
print (df.head())
when executing the above code I'm getting the Invalid syntax error at the below line
with teradatasql.connect(host='abc.abc.net', user='abcabc', password='abce123') as connect;
^
can you help me with the code to connect to teradata
Thanks in Advance.
Replace the a semicolon (;) with a colon (:)
The correct syntax for with is:
with expression [as variable]:
with-block
In your case:
with teradatasql.connect(host='abc.abc.net', user='abcabc', password='abce123') as connect:
query = ...
I'm trying to upload a dataframe in SQL server using pandas (to_sql) function, I get the below error
[SQL Server Native Client 11.0]Invalid character value for cast
specification (0) (SQLExecDirectW)')
I checked for variables' names and types and they are exactly the same in the SQL database and pandas dataframe.
How can I fix this?
Thanks
df.to_sql(raw_table, connDB, if_exists='append', index=False )
plz try this , this code use to juypter note book and SQL workbench
import mysql.connector
from mysql.connector import Error
from sqlalchemy import create_engine
import pandas as pd
mydata = pd.read_csv("E:\\Hourly_Format\\upload.csv")
engine = create_engine("mysql://root:admin#localhost/pythondb", pool_size=10, max_overflow=20)
mydata.to_sql(name='emp',con=engine,if_exists='append', index=False)
jupyter :-
workbench :-
I am trying to use fast_executemany to speed up my df.to_sql insert.
I read the documentation and added it to my code like this:
import pandas as pd
import sqlalchemy
import numpy as np
import random
#connect to database
server = 'Test'
database = 'Test'
driver = 'SQL+Server'
driver1 = 'ODBC+Driver+13+for+SQL+Server'
engine_stmt = ("mssql+pyodbc://#%s/%s?driver=%s" % (server, database, driver))
engine = sqlalchemy.create_engine(engine_stmt, fast_executemany=True)
connection = engine.connect()
When I run this code without the fast_executemany it works, but it takes fairly long for the insert.
Therefore I wanted to use the command, but I get an error when using it with the 'SQL+Server' driver. Therefore I tried to change the driver according to the documentation to 'ODBC+Driver+13+for+SQL+Server' I get the following error:
def create_connect_args(self, url):
InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
So I guess this driver is not working for me? I tested a few different ones, but the only one that is working is the 'SQL+Server'
It looks like you're missing the ODBC 13 driver on your computer.
Try installing that and run your script again.
Alternatively, try swapping:
driver1 = 'ODBC+Driver+13+for+SQL+Server'
for
driver1 = 'ODBC+Driver+17+for+SQL+Server'
Using Python: when connecting to SQL Server using pyodbc, everything works fine, but when I switch to sqlalchemy, the connection fails, giving me the error message:
('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
My code:
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=servername;DATABASE=dbname;UID=username;PWD=password')
engine = sqlalchemy.create_engine("mssql+pyodbc://username:password#servername/dbname")
I can't find the error in my code, and don't understand why the first options works, but the second doesn't.
Help is highly appreciated!
Ran into this problem as well, appending a driver query string to the end of my connection path worked:
"mssql+pyodbc://" + uname + ":" + pword + "#" + server + "/" + dbname + "?driver=SQL+Server"
Update (July 2021) – As above, just modernized (Python 3.6+):
f"mssql+pyodbc://{uname}:{pword}#{server}:{port}/{dbname}?driver=ODBC+Driver+17+for+SQL+Server"
Note that driver= must be all lowercase.
It works using pymssql, instead of pyodbc.
Install pymssql using pip, then change your code to:
engine = sqlalchemy.create_engine("mssql+pymssql://username:password#servername/dbname")
Very late, but experienced the same such problem myself recently. Turned out it was a problem with the latest SQLAlchemy version. Had to rollback my version from 1.4.17 to 1.4.12 (unsure of in-between versions, just went with a version I knew worked).
pip install sqlalchemy==1.4.12
I had original poster's problem with a trusted connection to the Microsoft SQL Server database (pandas 1.5.3, SQLAlchemy 2.0.4). Using answers from this question, this did the trick for me:
import sqlalchemy
import pandas as pd
server = "servername"
database = "dbname"
driver = "ODBC+Driver+17+for+SQL+Server"
url = f"mssql+pyodbc://{server}/{database}?trusted_connection=yes&driver={driver}"
engine = sqlalchemy.create_engine(url)
query = """
SELECT [column1]
,[column2] as some_other_name
FROM [server].[dbo].[table]"""
with engine.begin() as conn:
sqla_query = sqlalchemy.text(query)
df = pd.read_sql(sqla_query, conn)
It should be noted that pandas is not yet fully compatible with SQLAlchemy 2.0: https://pandas.pydata.org/docs/whatsnew/v1.5.3.html
I'm looking to connect to a local database called climat, the attributes of which are declared in my .odbc.ini file as follows
[ODBC Data Sources]
climat=Ingres
[climat]
Description=ingres database climat
Driver=Ingres
Server=nu
Database=climat
ServerType=Ingres
Servername=climat
When I try to run a pyodbc request using the following code:
import pyodbc
cxn=pyodbc.connect('DSN=climat')
I get the error:
pyodbc.Error: ('0', '[0] [unixODBC][ (786742) (SQLDriverConnectW)')
Does anyone have any suggestions as to what I am doing wrong?
Many thanks in advance for your help!