pyODBC和SQL Server 2008和Python 3 [英] pyODBC and SQL Server 2008 and Python 3

查看:75
本文介绍了pyODBC和SQL Server 2008和Python 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Python 3.2安装了pyODBC,并且正在尝试更新作为测试创建的SQL Server 2008 R2数据库.

I have pyODBC installed for Python 3.2 and I am attempting to update a SQL Server 2008 R2 database that I created as a test.

我没有问题,并且可以正常工作.

I have no problem retrieving data and that has always worked.

但是,当程序执行cursor.execute("sql")来插入或删除行时,它将不起作用-没有错误,什么也没有.响应就好像我已成功更新数据库,但未反映出任何更改.

However when the program performs a cursor.execute("sql") to insert or delete a row then it does not work - no error, nothing. The response is as if I am successful updating the database but no changes are reflected.

下面的代码本质上是在创建字典(稍后我有计划),并且只是在快速构建sql插入语句(在我测试写入日志的条目时起作用)

The code below essentially is creating a dictionary (I have plans for this later) and just doing a quick build of a sql insert statement (which works as I testing the entry I wrote to the log)

我的表Killer中有11行,即使在提交之后也完全没有受到影响.

I have 11 rows in my table, Killer, which is not being affected at all, even after a commit.

我知道这有点蠢,但我看不到.

I know this is something dumb but I can't see it.

这是代码:

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

推荐答案

假设这不是帖子中的错字,看起来您只是在

Assuming this isn't a typo in the post, looks like you're just missing parentheses for the Connection.commit() method:

...
# delete a row
rowsdeleted = cursor.execute("DELETE FROM Killer WHERE Id > 1").rowcount
logging.debug("deleted: " + str(rowsdeleted))

cnxn.commit()

这篇关于pyODBC和SQL Server 2008和Python 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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