为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】: DM Database Server 64 V8
【操作系统】:centos
【CPU】:虚拟机
【问题描述】*:在使用SqlAlchemy_dm插入数据时会提示 “TypeError: 'NoneType' object is not subscriptable” 而无法插入数据。
按照连接 https://eco.dameng.com/document/dm/zh-cn/app-dev/python-SQLAlchemy.html 中所提供的数据结构也无法插入数据。
部分错误栈如下:
2023-03-27 17:10:38,396 INFO sqlalchemy.engine.base.Engine INSERT INTO product ("NAME", "AUTHOR", "PUBLISHER", "PUBLISHTIME", "PRODUCTNO", "SATETYSTOCKLEVEL", "ORIGINALPRICE", "NOWPRICE", "DISCOUNT", "DESCRIPTION", "TYPE", "PAPERTOTAL", "WORDTOTAL", "SELLSTARTTIME", "SELLENDTIME") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
INFO:sqlalchemy.engine.base.Engine:INSERT INTO product ("NAME", "AUTHOR", "PUBLISHER", "PUBLISHTIME", "PRODUCTNO", "SATETYSTOCKLEVEL", "ORIGINALPRICE", "NOWPRICE", "DISCOUNT", "DESCRIPTION", "TYPE", "PAPERTOTAL", "WORDTOTAL", "SELLSTARTTIME", "SELLENDTIME") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2023-03-27 17:10:38,396 INFO sqlalchemy.engine.base.Engine ['水浒传', '施耐庵,罗贯中', '中华书局', '2005-4-1', '9787101046137', '10', '19', '14.3', '7.5', ' 《水浒传》是宋江起义故事在民间长期流传基础上产生出来的,吸收了民间文学的营养。', '16', '922', '912000', '2006-03-20', '']
INFO:sqlalchemy.engine.base.Engine:['水浒传', '施耐庵,罗贯中', '中华书局', '2005-4-1', '9787101046137', '10', '19', '14.3', '7.5', ' 《水浒传》是宋江起义故事在民间长期流传基础上产生出来的,吸收了民间文学的营养。', '16', '922', '912000', '2006-03-20', '']
最后一个id 0
SQL语句: select PRODUCTID from product where rowid = 0
2023-03-27 17:10:38,406 INFO sqlalchemy.engine.base.Engine ROLLBACK
INFO:sqlalchemy.engine.base.Engine:ROLLBACK
Traceback (most recent call last):
File "/home/test/dm_sqlalchemy.py", line 94, in <module>
main()
File "/home/test/dm_sqlalchemy.py", line 30, in main
fun_insert(DBSession)
File "/home/test/dm_sqlalchemy.py", line 72, in fun_insert
session.commit()
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 1046, in commit
self.transaction.commit()
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 504, in commit
self._prepare_impl()
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 483, in _prepare_impl
self.session.flush()
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2540, in flush
self._flush(objects)
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2682, in _flush
transaction.rollback(_capture_exception=True)
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__
compat.raise_(
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
raise exception
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2642, in _flush
flush_context.execute()
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/unitofwork.py", line 586, in execute
persistence.save_obj(
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 239, in save_obj
_emit_insert_statements(
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/orm/persistence.py", line 1135, in _emit_insert_statements
result = cached_connections[connection].execute(
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1011, in execute
return meth(self, multiparams, params)
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1124, in _execute_clauseelement
ret = self._execute_context(
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1316, in _execute_context
self._handle_dbapi_exception(
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1514, in _handle_dbapi_exception
util.raise_(exc_info[1], with_traceback=exc_info[2])
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 182, in raise_
raise exception
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1294, in _execute_context
result = context._setup_crud_result_proxy()
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy_dm-1.1.10-py3.8.egg/sqlalchemy_dm/base.py", line 1473, in _setup_crud_result_proxy
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 1253, in _setup_crud_result_proxy
self._setup_ins_pk_from_lastrowid()
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy_dm-1.1.10-py3.8.egg/sqlalchemy_dm/base.py", line 1507, in _setup_ins_pk_from_lastrowid
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy_dm-1.1.10-py3.8.egg/sqlalchemy_dm/base.py", line 1508, in <listcomp>
File "/opt/local/python/lib/python3.8/site-packages/sqlalchemy_dm-1.1.10-py3.8.egg/sqlalchemy_dm/base.py", line 1488, in _set_autoinc_col_from_lastrowid
TypeError: 'NoneType' object is not subscriptable
其中的中文 最后一个id 0, SQL语句: 两行为我自己为了debug添加的日志输出,并不影响原始代码的执行和功能。
经过排查是在如下图所示的地方,插入数据后返回的lastrowid不正确导致的无法插入数据和返回插入数据的id。
请问大家有遇到么,要怎么解决这个问题呢?
使用附件中的SqlAlchemy驱动 即可
trunk-348-20230306-python.zip