2014-03-25 59 views
0

这是我的游标执行代码。Django游标不插入/更新查询

from django.db import connection  
cursor = connection.cursor()  
cursor.execute("Some insert/Update query") 

游标执行成功,因为它作为插入的ID返回输出。但它没有在数据库中显示插入的值。

我试图关闭连接但没有成功。

cursor.close() 
Connection.close() 

如果我试图使用Connection.commit()但它给出了错误。

Exception Type: TransactionManagementError 
Exception Value: This code isn't under transaction management 

我发现了一个令人惊讶的行为。 如果我使用Connection.commit,那么它会给出错误,但它会成功插入行。 没有connection.commit它既不会提供错误,也不会在数据库中插入行。

我正在使用MS-SQL服务器作为使用sqlserver_ado的后端连接。

回答

0

大量的护目镜终于来到这个解决方案。

在视图调用之前使用@transaction.commit_manually

有关详细信息:Commit_Manually

0

你的Django版本应该低于1.6

应提交其手动 使用

django.db.transaction.commit_unless_managed() 

import django 
if not django.get_version().startswith('1.6'): 
    # when version=1.5 not auto-commit, commit here 
    django.db.transaction.commit_unless_managed()