Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a database using pymssql

Im trying to create a database using pymssql and im getting this error.

cur.execute("CREATE DATABASE %s;" % self.getsql('dbname'), conn)

gives

*** OperationalError: (226, 'CREATE DATABASE statement not allowed within multi-
statement transaction.DB-Lib error message 226, severity 16:\\nGeneral SQL Serve
r error: Check messages from the SQL Server\\n')

What does this mean ??

like image 672
jagguli Avatar asked Mar 29 '12 01:03

jagguli


People also ask

What driver does Pymssql use?

Python driver for SQL Server - Python driver for SQL Server Connect to a SQL Database using Python on Windows, Linux, or macOS.

What is Pymssql in Python?

A simple database interface for Python that builds on top of FreeTDS to provide a Python DB-API (PEP-249) interface to Microsoft SQL Server. The 2. x branch of pymssql is built on the latest release of FreeTDS which removes many of the limitations found with older FreeTDS versions and the 1. x branch.


1 Answers

The issue was that cur.execute starts a transaction every time, but 'CREATE DATABASE' operation cannot be excuted within a transaction

http://social.msdn.microsoft.com/Forums/pl/adodotnetdataproviders/thread/594ff024-8af6-40b3-89e0-53edb3ad7245

>>> connection.autocommit(True)
>>> cursor = connection.cursor()
>>> cursor.execute("CREATE DATABASE Foo")
>>> connection.autocommit(False)

This to works. Strangely its not documented in pymssql ... hmmm

like image 101
jagguli Avatar answered Sep 22 '22 12:09

jagguli