python 图书搜索算法
python book searching algorithm
我在 python 搜索查询中遇到错误:
return eval(raw_input(prompt))File "<string>", line 1 Bel and the Dragon
^
SyntaxError: unexpected EOF while parsing
这是我的代码:
from mysql.connector import MySQLConnection, Error
def searchBook():
search = input('Search Book?')
search = '%' + search + '%'
try:
conn = MySQLConnection(host='localhost',
database='python_mysql',
user='root',
password='')
query = 'SELECT * FROM books where title LIKE %s'
data = (search, search)
cursor = conn.cursor()
cursor.execute(query, data)
row = cursor.fetchall()
print(row)
except Error as error:
print(error)
if __name__ == '__main__':
searchBook()
稍微分解一下,
s = raw_input(prompt) # prompt user to enter a string
res = eval(s) # try to evaluate the string as a Python expression ?!
所以如果用户输入 4 + 6
,eval
将 运行 它和 return 10
.
Bel and the Dragon
不是 Python 表达式, 因此 尝试 eval
会给出语法错误。
我在 python 搜索查询中遇到错误:
return eval(raw_input(prompt))File "<string>", line 1 Bel and the Dragon
^
SyntaxError: unexpected EOF while parsing
这是我的代码:
from mysql.connector import MySQLConnection, Error
def searchBook():
search = input('Search Book?')
search = '%' + search + '%'
try:
conn = MySQLConnection(host='localhost',
database='python_mysql',
user='root',
password='')
query = 'SELECT * FROM books where title LIKE %s'
data = (search, search)
cursor = conn.cursor()
cursor.execute(query, data)
row = cursor.fetchall()
print(row)
except Error as error:
print(error)
if __name__ == '__main__':
searchBook()
稍微分解一下,
s = raw_input(prompt) # prompt user to enter a string
res = eval(s) # try to evaluate the string as a Python expression ?!
所以如果用户输入 4 + 6
,eval
将 运行 它和 return 10
.
Bel and the Dragon
不是 Python 表达式, 因此 尝试 eval
会给出语法错误。