PyODBC return error, but why?

我的未来我决定 提交于 2019-12-05 20:10:11

For reasons I don't quite understand, setting autocommit to be true seems to fix the issue. Please note that escaping the backslashes is still required.

Autocommit can be set in two ways:

cxnn=pyodbc.connect('DSN=SERVER;UID=sa;PWD=password', autocommit=True)

Or:

cxnn=pyodbc.connect('DSN=SERVER;UID=sa;PWD=password')
cxnn.autocommit = True

Once that's done, creating the cursor and executing the query should then run as expected:

cur=cxnn.cursor()
cur.execute("USE master;CREATE DATABASE Sales ON (NAME=Sales_dat, FILENAME='C:\\saledat.mdf', SIZE=10, MAXSIZE=50, FILEGROWTH=5) LOG ON (NAME=Sales_log, FILENAME='C:\\salelog.ldf', SIZE=5MB, MAXSIZE=25MB, FILEGROWTH=5MB );")

In my case I simply needed to upgrade pyodbc for it to work.

pip install --upgrade pyodbc
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!