Reputation: 1257
This is the code that I have
import time
import urllib
import openpyxl
from sqlalchemy import create_engine, Column, Integer, String, text, Float
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from astropy.table.tests.test_index import engine
from _sqlite3 import connect
connection_string = "DRIVER = {SQL Server};DATABASE =<dbname>; SERVER=<details>; UID =uid; PWD =pwd"
connection_string = urllib.parse.quote_plus(connection_string)
connection_string = "mssql+pyodbc:///?odbc_connect=%s" %connection_string
#engine = create_engine('mssql+pyodbc://UID:PWD@Servername:1433/DBname')
engine = create_engine(connection_string)
Base = declarative_base()
class Blc(Base):
__tablename__ = 'BLC_Database'
Row_no = Column(Integer, primary_key = True)
date = Column(String)
RES = Column(String)
BTTLCOLUMN = Column(String)
CS_HR = Column(Float)
def __repr__(self):
return "<BLC_Databse(Row_no = '%d', date='%s', RES = '%s', BTTLCOLUMN = '%s', CS_HR = '%0.2f')>" %(self.Row_no, self.date, self.RES, self.BTTLCOLUMN, self.CS_HR)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
print('Connection Successful')
The error that I am getting is as follows:
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I am trying to connect to a SQL server. I tried different string options. I visited this link too. I am not able to understand the error.
Upvotes: 1
Views: 1077
Reputation: 1257
Here is what I did:
I went to ODBC administration and created a new DSN and used that to connect.
create_engine('mssql+pyodbc://UID:PWD@DSN')
You dont need to mention the driver. You can assign a default database while configuring your DSN if you have multiple databases in one server. This worked for me.
Upvotes: 1