Python和Sqlite使用带有LIKE%var%command

时间:2015-12-24 03:40:32

标签: python sqlite

我在脚本中遇到了一些语法问题。

这有效:

c.execute('SELECT * FROM restaurants WHERE address LIKE "%Yongsan%"')

但是,我希望将"%Yongsan%"替换为raw_input变量,我无法弄清楚'%'符号的语法。

所以,有些事情......

t = raw_input("Enter your District: ")

g = '%',t,'%',

c.execute('SELECT * FROM restaurants WHERE address LIKE ?', g)

但是,当我执行脚本时,我收到以下错误:

"sqlite3.ProgrammingError: Incorrect number of bindings supplied. 
The current statement uses 1, and there are 3 supplied."

1 个答案:

答案 0 :(得分:0)

如果我正确理解你,这应该有效。

c.execute('SELECT * FROM restaurants WHERE address LIKE "%%%s%%"' % (raw_input('Enter your district: '),))

如果我误解你想要做什么,请告诉我。