title: Backup MS SQL Database with pyodbc
body: <p>I am trying to backup my database with pyodbc.</p>
  
  <p>The following SQL code has been tested and worked well directly in SQL Server Management Studio</p>
  
  <pre><code>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
  </code></pre>
  
  <p>And the code below is what I've tried in Python</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>However, the above code generates an error:</p>
  
  <blockquote>
    <p>pyodbc.Error: ('HY007', '[HY007] [Microsoft][ODBC SQL Server
    Driver]Associated statement is not prepared (0) (SQLNumResultCols)')</p>
  </blockquote>
  
  <p>May I know what is wrong with my Python code?</p>