Sql server pyODBC和sqlserver2008以及python3
我已经为Python 3.2安装了pyODBC,我正在尝试更新作为测试创建的SQL Server 2008 R2数据库 我在检索数据方面没有问题,这一直都很有效 但是,当程序执行cursor.execute(“sql”)来插入或删除行时,它就不起作用了——没有错误,什么也没有。响应就像我成功地更新了数据库,但没有反映任何更改 下面的代码基本上是创建一个字典(我以后有计划),并快速构建一个sql insert语句(在测试写入日志的条目时起作用) 我的表中有11行Killer,即使在提交之后,它也不会受到任何影响 我知道这很愚蠢,但我看不出来 代码如下:Sql server pyODBC和sqlserver2008以及python3,sql-server,python-3.x,odbc,pyodbc,Sql Server,Python 3.x,Odbc,Pyodbc,我已经为Python 3.2安装了pyODBC,我正在尝试更新作为测试创建的SQL Server 2008 R2数据库 我在检索数据方面没有问题,这一直都很有效 但是,当程序执行cursor.execute(“sql”)来插入或删除行时,它就不起作用了——没有错误,什么也没有。响应就像我成功地更新了数据库,但没有反映任何更改 下面的代码基本上是创建一个字典(我以后有计划),并快速构建一个sql insert语句(在测试写入日志的条目时起作用) 我的表中有11行Killer,即使在提交之后,它也不
cnxn = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=PHX-500222;DATABASE=RoughRide;UID=sa;PWD=slayer')
cursor = cnxn.cursor()
# loop through dictionary and create insert entries
logging.debug("using test data to build sql")
for row in data_dictionary:
entry = data_dictionary[row]
inf = entry['Information']
dt = entry['TheDateTime']
stat = entry['TheStatus']
flg = entry['Flagg']
# create sql and set right back into row
data_dictionary[row] = "INSERT INTO Killer(Information, TheDateTime, TheStatus, Flagg) VALUES ('%s', '%s', '%s', %d)" % (inf, dt, stat, flg)
# insert some rows
logging.debug("inserting test data")
for row in data_dictionary.values():
cursor.execute(row)
# delete a row
rowsdeleted = cursor.execute("DELETE FROM Killer WHERE Id > 1").rowcount
logging.debug("deleted: " + str(rowsdeleted))
cnxn.commit
假设这不是文章中的打字错误,看起来您只是缺少了方法的括号:
非常感谢。太有趣了,我正盯着我的问题看——有趣的是python解析器没有警告或抱怨cnxn.commit不是一个属性,而是一个函数。再次感谢@Tab如果您是通过脚本运行的,则没有消息,但它会在解释器中打印对象的字符串表示。此时,我正在WingIDE中运行-.NET是我的最后一个平台,因此这是一个相当大的转换。
...
# delete a row
rowsdeleted = cursor.execute("DELETE FROM Killer WHERE Id > 1").rowcount
logging.debug("deleted: " + str(rowsdeleted))
cnxn.commit()