簡體   English   中英

pyODBC和SQL Server 2008和Python 3

[英]pyODBC and SQL Server 2008 and Python 3

我為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

假設這不是帖子中的拼寫錯誤,看起來你只是缺少Connection.commit()方法的括號:

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

cnxn.commit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM