Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我会得到;从表达式“预期”;当我';我正在尝试更新表中的一行?_Python_Python 3.x_Sqlalchemy - Fatal编程技术网

Python 为什么我会得到;从表达式“预期”;当我';我正在尝试更新表中的一行?

Python 为什么我会得到;从表达式“预期”;当我';我正在尝试更新表中的一行?,python,python-3.x,sqlalchemy,Python,Python 3.x,Sqlalchemy,我试图更新表中的一行,但得到一个参数错误 代码如下: inventory_host = InventoryHost(ipv4_addr=ipv4, ipv6_addr=ipv6, macaddr=mac, host_name=name) try: session.add(inventory_host) session.commit() except sqlalchemy.exc.IntegrityError: session.execute(update(invento

我试图更新表中的一行,但得到一个参数错误

代码如下:

inventory_host = InventoryHost(ipv4_addr=ipv4, ipv6_addr=ipv6, macaddr=mac, host_name=name)

try:
    session.add(inventory_host)
    session.commit()
except sqlalchemy.exc.IntegrityError:
    session.execute(update(inventory_host))
    session.commit()

session.close()
这就是我得到的错误:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "perception.py", line 77, in <module>
    main()
  File "perception.py", line 66, in main
    modules.nmap_parser.parse_nmap_xml(nmap_xml)
  File "/Users/arozar/Documents/Scripts_Code/Python-Projects/perception/modules/nmap_parser.py", line 128, in parse_nmap_xml
    session.execute(update(inventory_host))
  File "<string>", line 2, in update
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/dml.py", line 668, in __init__
    ValuesBase.__init__(self, table, values, prefixes)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/dml.py", line 183, in __init__
    self.table = _interpret_as_from(table)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/selectable.py", line 41, in _interpret_as_from
    raise exc.ArgumentError("FROM expression expected")
sqlalchemy.exc.ArgumentError: FROM expression expected
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“perception.py”,第77行,在
main()
文件“perception.py”,第66行,在main中
modules.nmap\u parser.parse\u nmap\u xml(nmap\u xml)
parse\u nmap\u xml中的文件“/Users/arozar/Documents/Scripts\u Code/Python Projects/perception/modules/nmap\u parser.py”,第128行
会话.执行(更新(库存\主机))
文件“”,第2行,正在更新中
文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site packages/sqlalchemy/sql/dml.py”,第668行,在__
ValuesBase.\uuuu init\uuuuu(自身、表格、值、前缀)
文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site packages/sqlalchemy/sql/dml.py”,第183行,在__
self.table=\u将\u解释为来自(表)
文件“/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sqlalchemy/sql/selectable.py”,第41行,在
raise exc.ArgumentError(“预期来自表达式”)
sqlalchemy.exc.ArgumentError:应来自表达式

session.add(inventory\u host)
适用于inventory\u host表中的新主机,但只要我尝试使用
session.execute(update(inventory\u host))
更新行,我就会收到错误。

update
将表名作为其第一个参数,而不是表类的实例。您要更新什么值?例如,如果要更新主机名,可以改为:

from sqlalchemy import update

# Ideally, just use your primary key(s) in your where clause; I'm not sure what they are
stmt = (update(InventoryHost).where(ipv4_addr=ipv4, ipv6_addr=ipv6, macaddr=mac)
        .values(host_name=name)    # updates the host_name, for example
session.execute(stmt)
...

@太棒了!很高兴你让它工作了。干杯