我在应该是一个非常简单的脚本时遇到一些麻烦。我只是尝试使用Python pyodbc模块创建一个新的SQL Server数据库。我试图传入的“sqlcommand”参数在我在SQL Server 2012中执行时工作得很好,但它从这个python脚本失败了。不知道出了什么问题,谁有任何想法?
import pyodbc, os
def create_db(folder, db_name):
unc = r'\\arcsql\SDE\{0}'.format(folder)
if not os.path.exists(unc):
os.makedirs(unc)
full_name = os.path.join(r'E:\SDE', folder, db_name)
conn = pyodbc.connect("driver={SQL Server}; server=ArcSQL; database=master; Trusted_Connection=yes", automcommit=True)
cursor = conn.cursor()
sqlcommand = """USE [master]
GO
CREATE DATABASE [{0}] ON PRIMARY
( NAME = N'{0}', FILENAME = N'{1}.mdf', SIZE = 4MB , MAXSIZE = 10MB, FILEGROWTH = 1MB )
LOG ON
( NAME = N'{0}_log', FILENAME = N'{1}_log.ldf', SIZE = 4MB , MAXSIZE = 10MB, FILEGROWTH = 10%)
GO
USE [{0}]
GO""".format(db_name, full_name)
print sqlcommand
cursor.execute(sqlcommand)
print 'Created "{0}"'.format(db_name)
if __name__ == '__main__':
#test
create_db('_test', 'py_db_test')
和错误:
>>>
USE [master]
GO
CREATE DATABASE [py_db_test2] ON PRIMARY
( NAME = N'py_db_test2', FILENAME = N'E:\SDE\_test\py_db_test2.mdf', SIZE = 4MB , MAXSIZE = 10MB, FILEGROWTH = 1MB )
LOG ON
( NAME = N'py_db_test2_log', FILENAME = N'E:\SDE\_test\py_db_test2_log.ldf', SIZE = 4MB , MAXSIZE = 10MB, FILEGROWTH = 10%)
GO
USE [py_db_test2]
GO
Traceback (most recent call last):
File "C:/Users/calebma/Desktop/create_sql_db.py", line 40, in <module>
create_db('_test', 'py_db_test2')
File "C:/Users/calebma/Desktop/create_sql_db.py", line 35, in create_db
cursor.execute(sqlcommand)
Error: ('08004', "[08004] [Microsoft][ODBC SQL Server Driver][SQL Server]Database 'py_db_test2' does not exist. Make sure that the name is entered correctly. (911) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'GO'. (102); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'GO'. (102)")
>>>
答案 0 :(得分:8)
GO
是batch terminator in SQL Server Management Studio。它在pyodbc
中没有任何意义。而是从您的脚本发出单独的命令。
更新代码:
import pyodbc, os
def create_db(folder, db_name):
unc = r'\\arcsql\SDE\{0}'.format(folder)
if not os.path.exists(unc):
os.makedirs(unc)
full_name = os.path.join(r'E:\SDE', folder, db_name)
conn = pyodbc.connect("driver={SQL Server}; server=ArcSQL; database=master; Trusted_Connection=yes", automcommit=True)
cursor = conn.cursor()
sqlcommand = """
CREATE DATABASE [{0}] ON PRIMARY
( NAME = N'{0}', FILENAME = N'{1}.mdf', SIZE = 4MB , MAXSIZE = 10MB, FILEGROWTH = 1MB )
LOG ON
( NAME = N'{0}_log', FILENAME = N'{1}_log.ldf', SIZE = 4MB , MAXSIZE = 10MB, FILEGROWTH = 10%)
""".format(db_name, full_name)
print sqlcommand
cursor.execute(sqlcommand)
print 'Created "{0}"'.format(db_name)
# Do stuff in the new database
conn = pyodbc.connect("driver={SQL Server}; server=ArcSQL; database={0}; Trusted_Connection=yes".format(db_name), automcommit=True)
if __name__ == '__main__':
#test
create_db('_test', 'py_db_test')
答案 1 :(得分:1)
对于有类似问题的其他人,您也错过了提交。
所以,你可以在开始时使用conn.autocommit = True或者在cursor.execute命令之后使用conn.commit()
我努力解决为什么我的存储过程无法运行但不会出现错误 - 我努力解决了我正在做一个cursor.commit()而不是conn.commit()< / p>
答案 2 :(得分:0)
对于在脚本中使用 GO 并希望单独运行每个脚本的其他人。
sqlCommands = sqlFile.split('GO;')
for command in sqlCommands:
cursor.execute(command)