文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Django实现原生SQL的一个问题

Django实现原生SQL的一个问题

时间:2009-08-21  来源:hkebao

1.如果要执行多表查询我不想使用ORM的话。就使用原生SQL吧!
接收到多个条件了!如何拼结如下的SQL呢?select * from t where condi like '%s%'

看下面的一篇国外文章

Hi * again, auto-response.

but first I have to thanks to Drakonen of the Django IRC channel for
try to help me.

The error ( I do no knoe why yet ) was in this line :

sql = sql + 'AND f.url like "%%%s%%"' % (forge)

by a very strange reason the solution is to separate that sentence in
two of them, and you get something like this :

sql = sql + 'AND f.url like "%%'+ '%s' % (forge) +'%%" '

Another example would be :

sql = sql + 'AND f.url like "%%%s%%" limit %s' % (forge,num)

but unfortunately it does not work too, to fix this you have to split
the instruction in two of them and you can use :

sql = sql + 'AND f.url like "%%'+ '%s' % (forge) +'%%" '
sql = sql + 'LIMIT %s' % num

I hope this help someone :D

On Mar 30, 10:11 am, Francisco Rivas <taken...@xxxxxxxxx> wrote:
> Hi *
>
> I am trying to pass a variable to a Raw SQL in Django with the
> following code :
> Note on the function : forge and num are variables that I am getting
> from an URL.
>
> From the Django error web :
>
> all_info []
> amount  []
> aux     {}
> forge   u'objectweb'
> forge_proj u'objectweb'
> format u'rss'
> num 0
> proj_forge []
>
> As you can see the function is receiving the correct values for each
> variable and the function is :
>
> def search_projects_by_forge(forge,num=0):
>   # Query modified  because of the new bbdd model
>   if (num != 0):
>     sql = 'SELECT p.name, pi.url '
>     sql = sql + 'FROM projects p, forges f, project_info pi '
>     sql = sql + 'WHERE p.forge_id = f.id '
>     sql = sql + 'AND p.id = pi.project_id '
>     sql = sql + 'AND f.url like "%%%s%%" LIMIT %s' % (forge,num)
>   else:
>     sql = 'SELECT p.name, pi.url '
>     sql = sql + 'FROM projects p, forges f, project_info pi '
>     sql = sql + 'WHERE p.forge_id = f.id '
>     sql = sql + 'AND p.id = pi.project_id '
>     sql = sql + 'AND f.url like "%%%s%%"'  % (forge)
>
>   cursor = connection.cursor()
>   cursor.execute(sql)
>   results = cursor.fetchall()
>
>   print sql
>   print results
>
>   return [{'name':r[0],'url':r[1]} for r in results]
>
> Django is returning this error :
>
> TypeError at /search/forges=objectweb&format=rss
>
> not enough arguments for format string
>
> Request Method:         GET
> Request URL:    http://melquiades/search/forges=objectweb&format=rss
> Exception Type:         TypeError
> Exception Value:
>
> not enough arguments for format string
>
> Exception Location:     /var/lib/python-support/python2.5/django/db/
> backends/__init__.py in last_executed_query, line 173
>
> More about this error :
> sql
> u'SELECT p.name, pi.url FROM projects p, forges f, project_info pi
> WHERE p.forge_id = f.id AND p.id = pi.project_id AND f.url like
> "%objectweb%"'
> start
> 1238399284.6009409
> stop
> 1238399284.6009769
>
> The thing is that Django is executing the SQL with the correct
> parameter actually (like a said before) putting this SQL instruction
> in MySQL it works perfectly.
>
> I am not sure if the error is about the unicode or something like
> that.
>
> I know that the f variable is not necessary, that is for testing
> purposes only, actually copying the error from the Django Web Page
> (that page where Django shows the error, debug page) into MySQL it
> works.
>
> I really do not know what more to do, I have been seen the pages that
> offer information about types in python and so on but I can not fix
> this.
>
> Any help would be great, thank you in advance and best regards.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@xxxxxxxxxxxxxxxx
To unsubscribe from this group, send email to
django-users+unsubscribe@xxxxxxxxxxxxxxxx
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en

解决方案:
  sql = sql + ' and i_app like  "%%' + '%s' % (i_app) + '%%" '

确保一点执行SQL都封装到M层去!
相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载