Python sqlalchemy writing data into SQL Server - python

I'm trying to connect to SQL Server database using sqlalchemy (with pyodbc) on Python 3.6 in Spyder. I get no error when running:
import sqlalchemy
engine = create_engine('mssql+pyodbc://admin:pass#SERVERNAME/Databasename')
But when I take the data and try to write data into the database:
d={'col1':[1,2],'col2':[3,4]}
df=pd.DataFrame(data=d)
df.to_sql('TestTable',con=engine,if_exists='append',index_label='Indx')
I get the following error:
InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002]
[Microsoft][ODBC Driver Manager] Data source name not found and no
default driver specified (0) (SQLDriverConnect)') (Background on this
error at: http://sqlalche.me/e/rvf5)
Thank you in advance!

Related

Python SQL Alchemy connection to MSSQL Error

I'm using below pypyodbc library in python to connect with our SQL Server Database and it works perfectly.
import pypyodbc as odbc
conn = odbc.connect('DRIVER={SQL
Server};SERVER=serverv1;DATABASE=CMS;UID=Test;PWD=Test')
Now I want to use SQL Alchemy library to upload my excel data to our SQL Server database, however when I apply the same connection properties, I got an error.
from sqlalchemy import create_engine
import pandas as pd
import os
#dialect+driver://username:password#host:port/database --> Syntax
engine = create_engine('mssql+pyodbc://Test:Test#serverv1/CMS')
cwd = os.getcwd()
XLFile = (cwd +'/FILES/MARCH.xlsx')
xl = pd.read_excel(XLFile,sheet_name='SALES')
xl.to_sql('WES', engine, if_exists='append', index=False)
print(xl)
I used the same log in info but unsuccessful in SQL Alchemy. I already tried some of the samples here but no luck. Please help need to establish my connection.
Error encountered.
File
"C:\Users\test\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\sqlalchemy\engine\default.py", line 597, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(Background on this error at: https://sqlalche.me/e/14/rvf5)
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")
if it doesn't work downgrade your sqlalchemy

writing dataframe to sql db using pyodbc gives error

I have a Synapse database from which i am reading some tables to create a dataframe. I am using the below to connect and read the database tables:
server = 'gbs-marcus-dev-dw.database.windows.net'
database = 'romeo'
username = 'romeouser'
password = 'romeopass'
driver = 'ODBC Driver 17 for SQL Server'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
# select command
query = ''' SELECT * FROM dbo.Masterdata''';
df_input_orig = pd.read_sql(query, cnxn)
pyodbc.drivers()
['SQL Server', 'ODBC Driver 17 for SQL Server']
Post creating the dataframes, i am trying to write the resultant dataframes back to the DB and i am following the below two approches both of which give different errors.
Approch 1:
engine = sqlalchemy.create_engine("mssql+pyodbc://romeouser:romeopass#gbs-marcus-dev-dw.database.windows.net/romeo?driver=ODBC+Driver+17+for+SQL+Server",echo=False)
Insight_instance.to_sql(name='Insight_Instance',con=engine, index=False, if_exists='append')
cursor.execute('commit')
Error for approch 1:
ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]111214;An attempt to complete a transaction has failed. No corresponding transaction found. (111214) (SQLEndTran)')
(Background on this error at: http://sqlalche.me/e/13/f405)
Approch 2:
import pyodbc
import sqlalchemy
import urllib
params = urllib.parse.quote_plus("DRIVER=driver;SERVER=gbs-marcus-dev-dw.database.windows.net;DATABASE=romeo;UID=romeouser;PWD=romeopass")
engine = sqlalchemy.create_engine("mssql+pyodbc://romeouser:romeopass#gbs-marcus-dev-dw.database.windows.net/romeo",echo=False)
engine.connect()
Insight_instance.to_sql(name='Insight_Instance',con=engine, index=False, if_exists='append')
Error from approch 2:
InterfaceError: (pyodbc.InterfaceError) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
(Background on this error at: http://sqlalche.me/e/13/rvf5)
I am quite new to this and have no clue where or what i am doing wrong.

Having trouble connecting access database to python using pyodbc- trying to read data

Trying to connect access database to python so I can read it and eventually analyze it for certain things. But I can't connect to it and tried different approaches still getting the same error. I am using pyodbc.
Both python and access are 32bit. I am using windows 10 and the access is through MS office.
import pyodbc
connStr=(
r"DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};"
r"C:\Users\jonat\Desktop\Eritrean.accdb;"
)
cnxn=pyodbc.connect(connStr)
cursor=cnxn.cursor()
cursor.execute('select * from MemberPayment')
for row in cursor.fetchall():
print (row)
The error I've been getting
File "c:/Users/jonat/Desktop/pyth/prac.py", line 9, in <module>
cnxn=pyodbc.connect(connStr)
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect); [IM002] [Microsoft][ODBC Driver Manager] Invalid connection string attribute (0)')

How can i connect to local advantage database using pyodbc in python?

As far as i can understand to use pyodbc you have to
cnxn = pyodbc.connect('DRIVER={Advantage ODBC Driver};SERVER=local;DataDirectory=\\AltaDemo\Demo\AltaPoint.add;DATABASE=AltaPoint;UID=admin;PWD=admin;ServerTypes=1;')
cursor = cnxn.cursor()
This is the error i get from the console when i run this
Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
The name of the driver is Advantage StreamlineSQL ODBC, so the bare minimum connect string is:
DRIVER={Advantage StreamlineSQL ODBC};DataDirectory=D:\Temp
Other optinal options are:
DefaultType=Advantage
User ID=xxx
Password=xxx
ServerTypes=2
AdvantageLocking=ON
CharSet=ANSI
Language=ANSI
Description=My ADS connection
Locking=Record
MaxTableCloseCache=25
MemoBlockSize=64
Rows=False
Compression=Internet
CommType=TCP_IP
TLSCertificate=
TLSCommonName=
TLSCiphers=
DDPassword=Dictionary Password
EncryptionType=
FIPS=False
TrimTrailingSpaces=True
SQLTimeout=600
RightsChecking=OFF
If you want to use the Local Server you have to pass ServerTypes=1 like you already have in your original string.
For more options and documentation see also:
The official documentation
ConnectionStrings.com

connect to Azure SQL database via pyodbc

I use pyodbc to connect to my local SQL database which works withoout problems.
SQLSERVERLOCAL='Driver={SQL Server Native Client 11.0};Server=(localdb)\\v11.0;integrated security = true;DATABASE=eodba;'
cnxn = pyodbc.connect(SQLSERVERLOCAL) #works
I try the connection to the azure sql database with:
SQLSERVERAZURE='Driver={SQL Server Native Client 10.0};Server=tcp:mydatbase.database.windows.net,1433;Database=mydb;Uid=myuser#myerver;Pwd=mypass;Encrypt=yes;Connection Timeout=30;'
cnxn = pyodbc.connect(SQLSERVERAZURE) #works not
what gives me the error:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Besides the suggestions that provided by meet-bhagdev who recommended to use pymssql dirve that mentioned in link, to resolve the error: Data source name not found and no default driver specified (0) (SQLDriverConnect)') that encountered, please update your connect string as below to see if it works.
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=yoursqlAzureServer.database.windows.net,1433', user='yourName#yoursqlAzureServer', password='Password', database='DBName')
Download ODBC driver from Microsoft website and try installing it. I guess then the problem should disappear.
Here is the link:
https://www.microsoft.com/en-us/download/details.aspx?id=50420

Categories