Skip to content

Difficulty connecting to MSSQL LocalDB #541

@felipebastosweb

Description

@felipebastosweb

I'm trying to connect to mssql but I'm getting the same error.

When I try with pyodbc everything works. Example Code:
import pyodbc
conn = pyodbc.connect("DRIVER={ODBC Driver 17 for SQL Server};SERVER=(localdb)\MSSQLLocalDB;DATABASE=BastosErp;Trusted_Connection=yes")
cursor = conn.cursor()
cursor.execute("CREATE TABLE Users (Id int IDENTITY(1,1) PRIMARY KEY, Username NVARCHAR(100), Password NVARCHAR(100));")
cursor.commit()
cursor.execute("INSERT INTO dbo.Users (Username, Password) VALUES (?, ?);", ('felipe', 'felipe'))
cursor.commit()
cursor.execute("SELECT * FROM Users;")
rows = cursor.fetchall()
for row in rows:
print(row)
cursor.close()
conn.close()
But when I try with Emmett it gives an error. Here is the example code:

from emmett import App
from emmett.orm import Database
app = App(name)
app.config.db.adapter = "mssql" # <---- force the driver (chatgpt's suggestion)

option 1

app.config.db.uri = "mssql://(localdb)\mssqllocaldb;Database=BastosErp;Trusted_Connection=True;MultipleActiveResultSets=true"

option 2

app.config.db.uri = "mssql://(localdb)\MSSQLLocalDB/BastosErp?trusted_connection=yes&driver=ODBC+Driver+17+for+SQL+Server"

other options not listed here

... continue

db = Database(app)
db.define_models(User, Country, Language, Zone, Address, Category, Blog, Post, Comment)

app.pipeline = [db.pipe]
...
I have tried several other combinations of connection strings but I always get the same error: pyodbc.InterfaceError in C:\Users\felip\AppData\Local\Programs\Python\Python312\Lib\site-packages\pydal\adapters\mssql.py pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')

the integration problem ...

File "C:\Users\felip\AppData\Local\Programs\Python\Python312\Lib\site-packages\emmett\orm\connection.py", line 151, in _connector_sync return self.adapter.connector()
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\felip\AppData\Local\Programs\Python\Python312\Lib\site-packages\pydal\adapters\mssql.py", line 81, in connector
return self.driver.connect(self.cnxn, **self.driver_args)

Simplifying the connection string the error becomes

app.config.db.uri = "mssql://(localdb)\MSSQLLocalDB/BastosErp"

pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
'%s=%s' % (ak, av) for (ak, av) in iteritems(argsdict)])
self.cnxn = 'SERVER=%s;PORT=%s;DATABASE=%s;UID=%s;PWD=%s;%s'
% (host, port, db, user, password, urlargs)

def connector(self):
return self.driver.connect(self.cnxn, **self.driver_args)

I can't decide which driver to use. There is ODBC 17, 18 and MSSQL. None of them worked.

I would like to know if this is a bug or some simple undocumented setting of Emmett.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions