使用python 3尝试烧瓶中的mysql查询
SELECT title FROM Book WHERE title LIKE '%booktitlehere%';
从我的搜索表单的输入中,这是尝试烧瓶实现
search_string = search.data['search']
cur = mysql.connection.cursor()
likeString = "'%" + search_string + "%'"
cur.execute('''SELECT title FROM Book WHERE title LIKE %s;''',likeString)
当转到包含此表单的页面
时,它无法解决此错误_mysql_exceptions.ProgrammingError:并非在字符串格式化期间转换所有参数
我尝试添加%%以逃避文字%
likeString = "'%%" + search_string + "%%'"
# print(like)
cur.execute("""SELECT title FROM Book WHERE title LIKE %s;""",likeString)
rv = cur.fetchall()
但仍然出现错误
_mysql_exceptions.ProgrammingError: not all arguments converted during string formatting
还尝试使用select
附近的双引号cur.execute("SELECT title FROM Book WHERE title LIKE %s;",likeString)
答案 0 :(得分:0)
likeString = "'%%" + search_string + "%%'"
cur.execute("""SELECT title FROM Book WHERE title LIKE %s;""", (likeString))
答案 1 :(得分:0)
示例:
cari = "%" + request.form['cari'] +"%"
conn.execute("SELECT nama, harga FROM barang where nama LIKE %s OR harga LIKE %s", (cari, cari))
答案 2 :(得分:0)
这对我有用,我使用%%,喜欢后应该有“”
cur.execute('从名称中选择*,例如“ %%% s %%”',[名字])
答案 3 :(得分:-1)
在sql命令中使用搜索字符串之前
.col-style-3 {
padding: 0px !important;
background-color: #fff;
border: solid 1px #e0e0e0;
border-radius: 5px;
text-align: center;
color: #000;
transition: 0.5s;
}
.col-style-3 > a {
color: #FFFFFF;
text-decoration: none;
}
.col-style-3:hover {
border: solid 1px #bebebe;
box-shadow: 0 5px 10px #adadad;
transition: 0.3s;
}
然后在sql命令中应用
search_string= f"%{search_string}%"
另外还可以降低它
cur.execute("SELECT * FROM books WHERE title LIKE :searching_string ORDER BY id ",{"searching_string":search_string})
search_string= f"%{search_string}%".lower()