开发者

Error while using like statement in django raw sql

I have a raw sql query which is always giving error while executing. Here is my query

Users.objects.raw('select target, username from users where location like \'%s%%\' and date(modified) = \'2011-06-14\'',[location])

I am taking the location = 'BUI开发者_如何学GoLD'

Location values would be 'BUILD_A', 'BUILD_B','BUILD_C'.

When I am executing the raw sql, below is the error I am getting.

DatabaseError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BUILD'%' and date(modified) = '2011-06-14'' at line 1")

In MySQL terms I need to execute the following query:

Select target, username from users where location like 'BUILD%' and target = '2011-06-14'

I have googled it but could not able to get it. Please some one help me


I have solved my problem in this way.

location = location + '%'

users_list = Users.objects.raw('select target, username from users where location like %s and date(modified) = %s',tuple([location,date]))

The above statement executes perfectly without any error and I can able to render the results in template also.


Below query works for me,

query = "Select target, username from users where location like 'BUILD%'"
results = Users.objects.raw(query)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜