[英]Why am I getting an error when using the UPDATE function with SQLAlchemy?
[英]Why am I getting “FROM expression expected” from SQLAlchemy when I'm trying to update a row in a table?
我正在嘗試更新表中的一行,但出現參數錯誤。
這是代碼:
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
session.add(inventory_host)
適用於ventory_host表中的新主機,但是當我嘗試使用session.execute(update(inventory_host))
更新該行時,我得到了錯誤。
update
將表名作為其第一個參數,而不是表類的實例。 您要更新什么值? 例如,如果要更新host_name,則可以執行以下操作:
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)
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.