#python-3.x
Вопрос:
Я хочу сохранить запись с помощью psycopg2 в Python3, вот как выглядит мой код:
# coding:utf8
import time
from sqlalchemy import Column, String, Integer
from dolphin.common.commonlogger import CommonLogger
from dolphin.common.db.DatabaseService import Base, dict_session_scope
logger = CommonLogger().get_logger()
class Sentence(Base):
__tablename__ = 'sentence'
id = Column(Integer, primary_key=True, autoincrement=True, nullable=False)
word_id = Column(Integer, primary_key=False, nullable=False)
created_time = Column(Integer, primary_key=False, nullable=False)
updated_time = Column(Integer, primary_key=False, nullable=False)
sentence_zh = Column(String, primary_key=False, nullable=False)
sentence = Column(String, primary_key=False, nullable=False)
def __repr__(self):
return str(self.__dict__)
def save(self, sentence, word_id, sentence_zh):
article = Sentence(
sentence=sentence,
sentence_zh=sentence_zh,
word_id=word_id,
created_time=int(time.time() * 1000),
updated_time=int(time.time() * 1000)
)
sentence_instance = Sentence()
article_count = sentence_instance.count(sentence, word_id)
if article_count == 0:
with dict_session_scope() as local_count_session:
local_count_session.add(article)
local_count_session.flush()
local_count_session.commit()
else:
logger.warn("this article already exists in database,title:")
return article
def count(self, sentence, word_id):
with dict_session_scope() as local_count_session:
query = local_count_session.query(Sentence).filter(Sentence.sentence == sentence, Sentence.word_id == word_id).count()
return query
когда я запускаю этот код, появляется ошибка, подобная этой:
msg = "%sn" % self.format(record)
File "/usr/local/lib/python3.9/logging/__init__.py", line 927, in format
return fmt.format(record)
File "/usr/local/lib/python3.9/logging/__init__.py", line 663, in format
record.message = record.getMessage()
File "/usr/local/lib/python3.9/logging/__init__.py", line 367, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
File "/usr/local/lib/python3.9/threading.py", line 930, in _bootstrap
self._bootstrap_inner()
File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 77, in _worker
work_item.run()
File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run
result = self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.9/site-packages/apscheduler/executors/base.py", line 125, in run_job
retval = job.func(*job.args, **job.kwargs)
File "/root/pydolphin/dolphin/biz/dict/fetch_centence.py", line 22, in fetch
fetcher.get_parse_word()
File "/root/pydolphin/dolphin/biz/dict/fetch_centence.py", line 31, in get_parse_word
fetch.parse_dict_web(word.word, word.id)
File "/root/pydolphin/dolphin/biz/dict/fetch_centence.py", line 51, in parse_dict_web
sentence.save(eng, word_id, zh)
File "/root/pydolphin/dolphin/common/db/dict/sentence.py", line 32, in save
article_count = sentence_instance.count(sentence, word_id)
File "/root/pydolphin/dolphin/common/db/dict/sentence.py", line 45, in count
return query
File "/usr/local/lib/python3.9/contextlib.py", line 137, in __exit__
self.gen.throw(typ, value, traceback)
File "/root/pydolphin/dolphin/common/db/DatabaseService.py", line 49, in dict_session_scope
logger.error("dict session management error", e)
Message: 'dict session management error'
Arguments: (ProgrammingError("(psycopg2.ProgrammingError) can't adapt type 'Tag'"),)
--- Logging error ---
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1705, in _execute_context
self.dialect.do_execute(
File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 691, in do_execute
cursor.execute(statement, parameters)
psycopg2.ProgrammingError: can't adapt type 'Tag'
Я прочитал код ошибки, но не понял, в чем дело. почему это должно было произойти? что мне следует сделать, чтобы устранить эту проблему?