3

Python 3.2 用に pyODBC をインストールしました。テストとして作成した SQL Server 2008 R2 データベースを更新しようとしています。

データの取得に問題はなく、常に機能しています。

ただし、プログラムがcursor.execute("sql")を実行して行を挿入または削除すると、機能しません-エラーも何もありません。応答は、データベースの更新に成功したかのようですが、変更が反映されていません。

以下のコードは基本的に、辞書を作成し (後でこれを計画しています)、SQL 挿入ステートメントを簡単に作成するだけです (これは、ログに書き込んだエントリをテストするときに機能します)。

テーブル、Killer には 11 行ありますが、コミット後もまったく影響を受けていません。

私はこれがばかげていることを知っていますが、私はそれを見ることができません。

コードは次のとおりです。

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
4

1 に答える 1