我正在编写一个脚本来查询本周内包含某个网址的文章。我无法插入网址,尝试使用LIKE过滤器检查某个网址。我尝试插入,因为我做了已发布的开始和结束周,但它似乎与双插值混淆。
澄清一下,脚本在%{http://foo.com/bar}%
部分查询
start_week = today - datetime.timedelta(today.weekday())
end_week = start_week + datetime.timedelta(7)
cursor = db.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("""SELECT * FROM table WHERE url LIKE '%{http://foo.com/bar}%'""", [start_week, end_week])
result = cursor.fetchall()
print len(result)
答案 0 :(得分:1)
我要做的是在字符串中添加百分比,然后在sql查询中插入
答案 1 :(得分:1)
pythong不喜欢括号,它与嵌套插值混淆。我建议在字符串中添加%s并在查询中插入它。
bar = '%http://foo.com/bar%'
cursor = db.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("""SELECT * FROM table WHERE url LIKE %s""", [bar])
result = cursor.fetchall()
享受!
答案 2 :(得分:0)
查询语句中有两个WHERE运算符。
在此行中将第二个WHERE更改为AND:
cursor.execute("""SELECT * FROM table WHERE published>=%s AND published<=%s WHERE url LIKE '%{$http://foo.com/bar}%'""", [start_week, end_week])
应该是这样的:
cursor.execute("""SELECT * FROM table WHERE published>=%s AND published<=%s AND url LIKE '%{$http://foo.com/bar}%'""", [start_week, end_week])