I'm trying to create a SQL Server database using pyodbc.
import pyodbc
server = 'AMR112\NAMED1'
database = 'msdb'
username = ''
password = 'mypassword'
abcd='yes'
ghi='False'
#driver = '{/usr/local/lib/libtdsodbc.so}' #for linux of windows
driver= '{ODBC Driver 13 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';PORT=1433;SERVER='+server+??';PORT=1443;DATABASE??='+database+';UID='+??username+';PWD='+ password+';trusted_connection='+ abcd+'; autocommit='+ ghi) cursor = cnxn.cursor()
cursor.execute("create database dbafgh")
row = cursor.fetchone()
if row:
print row
cursor.close()
It fails with this error
CREATE DATABASE statement not allowed within multi-statement transaction
It fails because the .execute
method starts a transaction and CREATE DATABASE
cannot be run within a transaction.
So is there any other way to execute a CREATE DATABASE
command using python?