【问题标题】:pypyodbc update number type fieldpypyodbc 更新号码类型字段
【发布时间】:2016-06-09 20:41:36
【问题描述】:

我已使用下面的脚本成功更新了 Access .mdb 数据库中的文本字段。我正在尝试更新数字类型字段,但出现错误。

import csv, pypyodbc
conn=pypyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=c:/MDBTest/MyReducedMdb.mdb;')
cursor=conn.cursor()
LUT = {}
with open("C:\MDBTest\order.txt") as file:
    for line in file:
        (key, val) = line.split()
        LUT [key] = val
print (LUT)
cursor.execute("select * from Components")
rows = cursor.fetchall()
for row in rows:
    warehousecode=row[6].strip()
    if warehousecode in LUT:
        cursor.execute("""
                        UPDATE Components
                        SET Order=?
                        Where [Component Key]=?;""", (int(LUT[warehousecode]), str(row[0])))
        cursor.commit()

当我运行代码时,出现以下错误。

{'406-007': '2', '406-012': '4', '406-005': '1', '406-015': '5', '406-010': '3'}
Traceback (most recent call last):
  File "D:\My Python\Scripts\IrricadDatabaseOrderUpdate.py", line 18, in <module>
    Where [Component Key]=?;""", (int(LUT[warehousecode]), str(row[0])))
  File "C:\Python34\lib\pypyodbc.py", line 1596, in execute
    check_success(self, ret)
  File "C:\Python34\lib\pypyodbc.py", line 986, in check_success
    ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
  File "C:\Python34\lib\pypyodbc.py", line 954, in ctrl_err
    raise ProgrammingError(state,err_text)
pypyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement.')
>>>

请让我知道您认为问题可能出在哪里。谢谢。

【问题讨论】:

    标签: pypyodbc


    【解决方案1】:

    您的问题是ORDER 是保留的SQL 关键字。用引号括起来,以避免你的 SQL 驱动程序混淆。

    """
        UPDATE Components
        SET "Order"=?
        Where [Component Key]=?;"""
    

    并让您的数据库设计师使用 SQL 关键字作为列名。

    【讨论】:

    • 好答案 (+1),但使用方括号更能代表 Access SQL 的常见做法,并且与同一查询中 [Component Key] 字段的引用一致。
    猜你喜欢
    • 2018-01-26
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多