SQLAlchemy核心与Python中的PostgreSQL,连接.execute(..)错误。

huangapple go评论72阅读模式
英文:

sqlalchemy core with postgresql in python, conneting.execute(..) error

问题

我正在学习在Python中使用PostgreSQL数据库的SQLAlchemy核心。

我尝试运行以下脚本,并收到以下错误消息:

from sqlalchemy import create_engine  
from sqlalchemy import Table, MetaData, String

engine = create_engine('postgresql://postgres:123456@localhost:5432/red30')

with engine.connect() as connection:
    meta = MetaData(engine)  
    sales_table = Table('sales', meta)

    # 创建
    insert_statement = sales_table.insert().values(order_num=1105911, 
                                                order_type='Retail', 
                                                cust_name='Syman Mapstone', 
                                                prod_number='EB521', 
                                                prod_name='Understanding Artificial Intelligence', 
                                                quantity=3, 
                                                price=19.5, 
                                                discount=0, 
                                                order_total=58.5)
    connection.execute(insert_statement)

    # 读取
    select_statement = sales_table.select().limit(10)
    result_set = connection.execute(select_statement)
    for r in result_set:
        print(r)

    # 更新
    update_statement = sales_table.update().where(sales_table.c.order_num==1105910).values(quantity=2, order_total=39)
    connection.execute(update_statement)

    # 确认更新:读取
    reselect_statement = sales_table.select().where(sales_table.c.order_num==1105910)
    updated_set = connection.execute(reselect_statement)
    for u in updated_set:
        print(u)

    # 删除
    delete_statement = sales_table.delete().where(sales_table.c.order_num==1105910)
    connection.execute(delete_statement)

    # 确认删除:读取
    not_found_set = connection.execute(reselect_statement)
    print(not_found_set.rowcount)

错误消息:

(postgres-prac) E:\xfile\postgresql\postgres-prac>python postgres-sqlalchemy-core.py
Traceback (most recent call last):
  File "postgres-sqlalchemy-core.py", line 20, in <module>
    connection.execute(insert_statement)
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\engine\base.py", line 1414, in execute
    return meth(
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\elements.py", line 485, in _execute_on_connection
    return connection._execute_clauseelement(
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\engine\base.py", line 1630, in _execute_clauseelement
    compiled_sql, extracted_params, cache_hit = elem._compile_w_cache(
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\elements.py", line 651, in _compile_w_cache
    compiled_sql = self._compiler(
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\elements.py", line 290, in _compiler
    return dialect.statement_compiler(dialect, self, **kw)
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\compiler.py", line 1269, in __init__
    Compiled.__init__(self, dialect, statement, **kwargs)
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\compiler.py", line 710, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\compiler.py", line 755, in process
    return obj._compiler_dispatch(self, **kwargs)
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\visitors.py", line 143, in _compiler_dispatch
    return meth(self, **kw)  # type: ignore  # noqa: E501
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\compiler.py", line 5317, in visit_insert
    crud_params_struct = crud._get_crud_params(
  File "E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\crud.py", line 326, in _get_crud_params
    raise exc.CompileError(
sqlalchemy.exc.CompileError: Unconsumed column names: order_type, quantity, cust_name, discount, prod_number, price, order_total, order_num, prod_name

如有需要,我可以提供有关此错误的解释或建议。

英文:

I am learning sqlalchemy core with postgresql database in python.

I tried to run the following script and got this error message:

from sqlalchemy import create_engine  
from sqlalchemy import Table, MetaData, String

engine = create_engine(&#39;postgresql://postgres:123456@localhost:5432/red30&#39;)

with engine.connect() as connection:
    meta = MetaData(engine)  
    sales_table = Table(&#39;sales&#39;, meta)

    # Create
    insert_statement = sales_table.insert().values(order_num=1105911, 
                                                order_type=&#39;Retail&#39;, 
                                                cust_name=&#39;Syman Mapstone&#39;, 
                                                prod_number=&#39;EB521&#39;, 
                                                prod_name=&#39;Understanding Artificial Intelligence&#39;, 
                                                quantity=3, 
                                                price=19.5, 
                                                discount=0, 
                                                order_total=58.5)
    connection.execute(insert_statement)

    # Read
    select_statement = sales_table.select().limit(10)
    result_set = connection.execute(select_statement)
    for r in result_set:
        print(r)

    # Update
    update_statement = sales_table.update().where(sales_table.c.order_num==1105910).values(quantity=2, order_total=39)
    connection.execute(update_statement)

    # Confirm Update: Read
    reselect_statement = sales_table.select().where(sales_table.c.order_num==1105910)
    updated_set = connection.execute(reselect_statement)
    for u in updated_set:
        print(u)

    # Delete
    delete_statement = sales_table.delete().where(sales_table.c.order_num==1105910)
    connection.execute(delete_statement)

    # Confirm Delete: Read
    not_found_set = connection.execute(reselect_statement)
    print(not_found_set.rowcount)

error message:

(postgres-prac) E:\xfile\postgresql\postgres-prac&gt;python postgres-sqlalchemy-core.py
Traceback (most recent call last):
  File &quot;postgres-sqlalchemy-core.py&quot;, line 20, in &lt;module&gt;
    connection.execute(insert_statement)
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\engine\ba
se.py&quot;, line 1414, in execute
    return meth(
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\eleme
nts.py&quot;, line 485, in _execute_on_connection
    return connection._execute_clauseelement(
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\engine\ba
se.py&quot;, line 1630, in _execute_clauseelement
    compiled_sql, extracted_params, cache_hit = elem._compile_w_cache(
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\eleme
nts.py&quot;, line 651, in _compile_w_cache
    compiled_sql = self._compiler(
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\eleme
nts.py&quot;, line 290, in _compiler
    return dialect.statement_compiler(dialect, self, **kw)
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\compi
ler.py&quot;, line 1269, in __init__
    Compiled.__init__(self, dialect, statement, **kwargs)
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\compi
ler.py&quot;, line 710, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\compi
ler.py&quot;, line 755, in process
    return obj._compiler_dispatch(self, **kwargs)
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\visit
ors.py&quot;, line 143, in _compiler_dispatch
    return meth(self, **kw)  # type: ignore  # noqa: E501
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\compi
ler.py&quot;, line 5317, in visit_insert
    crud_params_struct = crud._get_crud_params(
  File &quot;E:\xfile\postgresql\postgres-prac\lib\site-packages\sqlalchemy\sql\crud.
py&quot;, line 326, in _get_crud_params
    raise exc.CompileError(
sqlalchemy.exc.CompileError: Unconsumed column names: order_type, quantity, cust
_name, discount, prod_number, price, order_total, order_num, prod_name

答案1

得分: 1

你将表定义为空表格:

sales_table = Table('sales', meta)

所以,当尝试插入包含所有这些关键词的记录时,它们无法映射到列并且不会被消耗,因此会出现“未消耗的列名”错误。

你需要在创建Table时定义表格列。请参考以下来自文档的示例:

from sqlalchemy import Table, Column, Integer, String

user = Table(
    "user",
    metadata_obj,
    Column("user_id", Integer, primary_key=True),
    Column("user_name", String(16), nullable=False),
    Column("email_address", String(60)),
    Column("nickname", String(50), nullable=False),
)
英文:

You define your table as an empty table:

sales_table = Table(&#39;sales&#39;, meta)

So when trying to insert a record with all those keywords, they cannot be mapped to columns and do not get consumed, hence the Unconsumed column names error.

You need to define the table columns in your Table creation. See the following example from the docs:

from sqlalchemy import Table, Column, Integer, String

user = Table(
    &quot;user&quot;,
    metadata_obj,
    Column(&quot;user_id&quot;, Integer, primary_key=True),
    Column(&quot;user_name&quot;, String(16), nullable=False),
    Column(&quot;email_address&quot;, String(60)),
    Column(&quot;nickname&quot;, String(50), nullable=False),
)

huangapple
  • 本文由 发表于 2023年2月16日 19:34:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定