数据库中存在%的还有一些转义字符如:
这种字符
我们要查询:
发现报错:
这种原因是因为有转义字符;无论是pandas查询还是mysql查询,还是sqlalchemy查询,都会报这种错,因为他们都调用了原生的mysql查询语句
解决办法来了:
使用sqlalchemy的orm框架
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
class DataInsert(object):
def __init__(self):
self.engine = create_engine("""mysql+pymysql://root:mysql@127.0.0.1:3306/local?charset=utf8""")
Session = sessionmaker(bind=self.engine)
self.session = Session()
def selectsql(self, title=None, url=None):
sql = """select * from Google_data WHERE url = "http://www.ap1.se/upload/Upphandling/RFI/Request%20for%20information%20Ethical%20and%20environmental%20analysis%2014%2006%2018.pdf" """
data = self.session.execute(sql).fetchall()
return data
或者使用sqllachemy 的text 将sql转义
from sqlalchemy import text
sql = """select * from Google_data WHERE url = "http://www.ap1.se/upload/Upphandling/RFI/Request%20for%20information%20Ethical%20and%20environmental%20analysis%2014%2006%2018.pdf" """
self.engine.execute(text(sql)).fetchall()
建议使用sqllachemy库的转义语法,这个可以防止数据注入的安全问题