提前感谢您的帮助。我在 Stackoverflow 和 Google 上看到了几个解决这个问题的方法,但在实施时似乎没有一个对我有用。我只是想将包含在变量中的 python 字符串传递给 sqlite3 WHERE LIKE 查询。没有收到任何错误,只是没有屏幕输出。以下是我尝试过的代码:
尝试 1:导入 cgi 导入 sqlite3
#Get string/Clean string:
form = cgi.FieldStorage()
term = form.getfirst('term', 'empty')
term = cgi.escape(term)
#Open DB Connection:
conn = sqlite3.connect("mydatabase.db")
cursor = conn.cursor()
#Search and Print Results
print "\nResults from a LIKE query on %s:\n" % term
sql='SELECT * FROM myTable WHERE column LIKE %s'
args=['%'+term+'%']
cursor.execute(sql,args)
print cursor.fetchall()
Try2:导入 cgi 导入 sqlite3
#Get string/Clean string:
form = cgi.FieldStorage()
term = form.getfirst('term', 'empty')
term = cgi.escape(term)
#Open DB Connection:
conn = sqlite3.connect("mydatabase.db")
cursor = conn.cursor()
#Search and Print Results
print "\nResults from a LIKE query on %s:\n" % term
term = '%' + term + '%'
cursor.execute("SELECT * FROM table WHERE column LIKE ?", (term,) )
print cursor.fetchall()
如果我输入 LIKE '%http%',代码就可以工作,但变量总是失败。cgi和它有关系吗?如果我只是将术语打印到屏幕上,它会打印得很好。