如何在pymssql中执行.sql文件 [英] How to execute an .sql file in pymssql

查看:72
本文介绍了如何在pymssql中执行.sql文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pymssql在python中执行sql文件,该文件包含BEGIN TRANSACTION,COMMIT TRANSACTION和END,以及前后的一些安全网.

I'm trying to execute an sql file in python using pymssql, this file contains a BEGIN TRANSACTION, a COMMIT TRANSACTION and an END, and some safety nets before and after.

我正在尝试打开内存中的文件并执行内容:

I'm trying to open the file in memory and execute the content:

file = open(options.sqlFile, 'r')
sqlFileContents = file.read()
file.close()

cursor.execute(sqlFileContents)
conn.commit()

但是它向我返回了错误:

But it's returning me errors:

pymssql.ProgrammingError: (102, "Incorrect syntax near 'GO'.DB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 1
5:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity     
15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102,severity 
15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 102, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n")

所以基本上我有两个问题:

So basically I have two questions:

  1. 是否可以按照我的方式执行查询?
  2. sql查询文件本身是问题吗?

感谢您的帮助.

这是SQL:

这是SQL:

SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT,             QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE     id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
PRINT N'Adding Release Version to [admin].[ReleaseHistory]'
GO
INSERT INTO [admin].[ReleaseHistory] VALUES (GetUTCDate(), '1.7')
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN     TRANSACTION END
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO

推荐答案

是的,可以这样做.我经常这样做,在代码中对SQL进行硬编码会更干净.

Yes it is possible to do it like that. I often do so, it is cleaner that hardcoding the SQL in your code.

可以将SQL添加到帖子中吗?某处可能是一个混乱的角色.

Can you add the SQL to your post? there's probably a messed up character somewhere.

我这样做(用pyodbc):

I do it like that (with pyodbc):

with open('%smysql.sql' % SQL_DIR) as f: 
            sql = f.read() % params # Don't do that with untrusted inputs
            cursor.execute(sql)
            cursor.commit()
            cursor.close()

删除所有的 GO ,它不是真正的SQL语句.检查以下答案:使用"GO"在交易中.

Remove all the GO it's not an real SQL statement. Check this answer: Using "GO" within a transaction.

那应该没事的.

这篇关于如何在pymssql中执行.sql文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆