I am trying to detach the database, but for some reason it does not detach with no error, am I missing something?
JavaScript
x
15
15
1
import pyodbc
2
params = 'DRIVER={SQL Server};SERVER=.RISKSPEC_PSA2012;DATABASE=master;UID=sa;Pwd=sa_pasw;Trusted_Connection=yes;'
3
try:
4
with pyodbc.connect(params) as cnxn:
5
with cnxn.cursor() as cursor:
6
cnxn.autocommit = True
7
cursor.execute(
8
'''
9
USE [master];
10
ALTER DATABASE [sss] SET SINGLE_USER WITH NO_WAIT;
11
EXEC sp_detach_db @dbname=sss;
12
''')
13
except pyodbc.Error as ex:
14
QMessageBox.warning(self, 'pyodbc', ex.args[0])
15
SQLServer 2012 version: 11.0.2100
pyodbc version: 4.0.31
Advertisement
Answer
Thx for Gord Thompson for the tip.
Fixes:
- SET SINGLE_USER WITH NO_WAIT -> SET TRUSTWORTHY ON
- Remove from connect params “Trusted_Connection=yes;”, It associates the system user instead of “sa” user.
JavaScript
1
15
15
1
import pyodbc
2
params = 'DRIVER={SQL Server};SERVER=.RISKSPEC_PSA2012;DATABASE=master;UID=sa;Pwd=sa_pasw;'
3
try:
4
with pyodbc.connect(params) as cnxn:
5
cnxn.autocommit = True
6
with cnxn.cursor() as cursor:
7
cursor.execute(
8
'''
9
USE [master];
10
ALTER DATABASE [sss] SET TRUSTWORTHY ON;
11
EXEC sp_detach_db @dbname=sss;
12
''')
13
except pyodbc.Error as ex:
14
QMessageBox.warning(self, 'pyodbc', ex.args[0])
15