I am trying to backup my database with pyodbc.
The following SQL code has been tested and worked well directly in SQL Server Management Studio
DBCC SHRINKFILE(MyDB_v0_log, 100)
GO
BACKUP DATABASE comparables
TO DISK = N'D:\MSSQL\BACKUP\MyDB_v0_noFSD.bak' WITH NOFORMAT
, INIT
, NAME = N'backup_MyDB_v0_noFSD.bak', SKIP, REWIND, NOUNLOAD
, STATS = 10
And the code below is what I've tried in Python
conn = pyodbc.connect("driver={SQL Server};server=MyServer;database=MyDB;trusted_connection=true")
cursor = conn.cursor()
SQL_command = """
DBCC SHRINKFILE(comparables_v0_log, 100)
BACKUP DATABASE MyDB
TO DISK = N'D:\MSSQL\BACKUP\MyDB_v0_noFSD.bak' WITH NOFORMAT
, INIT
, NAME = N'backup_MyDB_v0_noFSD.bak', SKIP, REWIND, NOUNLOAD
, STATS = 10
"""
cursor.execute(SQL_command)
cursor.commit()
However, the above code generates an error:
pyodbc.Error: ('HY007', '[HY007] [Microsoft][ODBC SQL Server Driver]Associated statement is not prepared (0) (SQLNumResultCols)')
May I know what is wrong with my Python code?