1
구문과 어리석은 실수를하고있다. 그러나 무엇을 알아낼 수 없다.SQLAlchemy func.lower를 MySQL과 함께 사용할 수 없다.
나는 낮은 경우에 둘으로 표시하여 DB 필드와 변수를 비교하는 SQLAlchemy의func
을 사용하려고하지만, 조건이 만족되지 것처럼 출력은 널 (null) 제공됩니다.
func
을 사용하지 않고 정적 변수를 전달하면 똑같은 문제가 해결됩니다.
강령 :
question = "Hey"
q1 = QuestionsAndAnswers.query.filter(func.lower(QuestionsAndAnswers.question) == func.lower(question)).all()
q2 = QuestionsAndAnswers.query.filter(QuestionsAndAnswers.question == "Hey").all()
print "q1", q1
print "q2", q2
아웃풋 :
q1 []
q2 [<intercom_bot.models.QuestionsAndAnswers object at 0x7f1e2c7add50>]
DB를 :
+----+-----------------+--------------------------------------------------+------------+
| id | question | answer | created_at |
+----+-----------------+--------------------------------------------------+------------+
| 1 | Hi | Hey, Here I am and here you are. How can I help? | NULL |
| 2 | Hello | Hey, Here I am and here you are. How can I help? | NULL |
| 3 | Helo | Hey, Here I am and here you are. How can I help? | NULL |
| 4 | Heelo | Hey, Here I am and here you are. How can I help? | NULL |
| 5 | Hell | Hey, Here I am and here you are. How can I help? | NULL |
| 6 | Hallo | Hey, Here I am and here you are. How can I help? | NULL |
| 7 | Hey | Hey, Here I am and here you are. How can I help? | NULL |
| 8 | He | Hey, Here I am and here you are. How can I help? | NULL |
| 9 | Ho | Hey, Here I am and here you are. How can I help? | NULL |
| 10 | I need help | Hey, Here I am and here you are. How can I help? | NULL |
| 11 | Help | Hey, Here I am and here you are. How can I help? | NULL |
| 12 | can you help me | Hey, Here I am and here you are. How can I help? | NULL |
| 13 | Greetings | Hey, Here I am and here you are. How can I help? | NULL |
+----+-----------------+--------------------------------------------------+------------+
PS :
print QuestionsAndAnswers.query.filter(func.lower(QuestionsAndAnswers.question) == func.lower(question))
은 다음을 제공합니다 :
SELECT questions_and_answers.id AS questions_and_answers_id, questions_and_answers.question AS questions_and_answers_question, questions_and_answers.answer AS questions_and_answers_answer, questions_and_answers.created_at AS questions_and_answers_created_at
FROM questions_and_answers
WHERE lower(questions_and_answers.question) = lower(:lower_1)
PPS : 다음은 모델입니다.
class QuestionsAndAnswers(Base):
"""docstring for QuestionsAndAnswers"""
__tablename__ = 'questions_and_answers'
id = Column(BIGINT, primary_key=True)
question = Column(MEDIUMBLOB, nullable=False)
answer = Column(MEDIUMBLOB, nullable=False)
created_at = Column(DATETIME, nullable=True,
default=datetime.datetime.now())
이 그것을 사용할 수 있습니까 :
적절한 수정 모델, 테이블과 같은
Unicode
및UnicodeText
하지만 빠른 해결책이 캐스트를 추가하는 것 등의 적절한 텍스트 형식을 사용하는 데이터를 변경하는 것 '== question.lower()'를 사용한다면? – syntonym질의 자체를 검사 해보십시오 :'print (QuestionsAndAnswers.query.query.filter (fun.lower (QuestionsAndAnswers.question) == func.lower (question)))'그리고 이상한 것이 나타나는지보십시오. –
또한 MySQL을 사용하고 있습니까? 'question' 속성은 이진 문자열입니까? 바이너리 문자열 ('BINARY','VARBINARY','BLOB')에 적용될 때''LOWER()''(그리고''UPPER()'')는 효과가 없습니다. "] (http://dev.mysql.com/doc /refman/5.7/en/string-functions.html#function_lower). 적어도 당신은 파이썬 2를 사용하고 있습니다. 그래서 "Hey"는 바이트 문자열이므로'func.lower (question)'은 비효율적입니다. –