为什么这个查询不能在python中运行? [英] Why isn't this query working in python?

查看:62
本文介绍了为什么这个查询不能在python中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试运行以下查询:


amember_db = MySQLdb.connect(host =" ; localhost",user =" **********",

passwd =" *****",db =" ***** **")

#创建游标

self.amember_cursor = amember_db.cursor()

#执行SQL语句

sql =""" SELECT payment_id FROM amember_payments WHERE

member_id =%s AND expire_date NOW()AND completed = 1 AND(product_id


> 11 AND product_id< 21)""",(self.uid)



print sql

self.amember_cursor.execute(* sql)

amember_result = self.cursor.fetchall()

打印amember_result


当我在mysql中手动运行SQL查询时,我得到一个结果,但是对于

某些原因我在python中得到一个空结果。这里有一些注意事项

可能很重要。


-我在这个脚本中有2个连接打开MySQL(它们都在
中)
单独的对象当然)

- self.uid = 1972L

-print amember_result =()


Any想法?


谢谢!

Erik

解决方案

我正在尝试运行以下查询:

....


member_id =%s AND expire_date NOW()AND completed = 1 AND(product_id



难道你不能使用绑定变量'''''而不是''%s''?

(我在问,因为我不完全确定执行命令是怎样的

进行替换)


-Dave


5月25日上午10:51,Dave Borne< dbo ... @ gmail.comwrote:


我正在尝试运行以下查询:



...


member_id =%s AND expire_date NOW()AND completed = 1 AND(product_id



难道你不应该使用绑定变量''''''而不是''%s''?

(我问,因为我不完全确定执行命令是如何进行替换的b $ b) />

-Dave



嗨Dave,


我不确定。我一直在使用这种格式来解决所有其他问题

没有问题。相反,使用绑定变量

查询会是什么样子?


Erik


< blockquote> On Fri,2007-05-25 at 09:51 -0500,Dave Borne写道:


我正在尝试运行以下查询:



...


member_id =%s AND expire_date NOW() AND completed = 1 AND(product_id



难道你不能使用绑定变量'''''而不是''%s''?



MySQLdb的参数占位符确实是,不幸的是,%s。

OP正确使用参数替换,但在

混淆时尚。''sql''是一个名不副实的元组,包含查询

string *和*参数,这些参数正在用''*'解压缩​​为两个

执行调用的参数。

我看到的唯一问题是参数应该是一个序列,即

(self.uid),而不仅仅是(self.uid)。


HTH,

-

Carsten Haese
http://informixdb.sourceforge.net


Hi all,

I''m trying to run the following query:

amember_db = MySQLdb.connect(host="localhost", user="**********",
passwd="*****", db="*******")
# create a cursor
self.amember_cursor = amember_db.cursor()
# execute SQL statement
sql = """SELECT payment_id FROM amember_payments WHERE
member_id=%s AND expire_date NOW() AND completed=1 AND (product_id

>11 AND product_id <21)""", (self.uid)

print sql
self.amember_cursor.execute(*sql)
amember_result = self.cursor.fetchall()
print amember_result

When I manually run the SQL query in mysql, I get a result, but for
some reason I get an empty result in python. Here are some notes that
may be of importance.

-I have 2 connections open to MySQL in this script (they are both in
seperate objects of course)
- self.uid = 1972L
-print amember_result = ()

Any ideas?

Thanks!
Erik

解决方案

I''m trying to run the following query:
....

member_id=%s AND expire_date NOW() AND completed=1 AND (product_id

Shouldn''t you be using the bind variable ''?'' instead of ''%s'' ?
(I''m asking because I''m not entirely sure how the execute command is
doing the substitution)

-Dave


On May 25, 10:51 am, "Dave Borne" <dbo...@gmail.comwrote:

I''m trying to run the following query:

...

member_id=%s AND expire_date NOW() AND completed=1 AND (product_id


Shouldn''t you be using the bind variable ''?'' instead of ''%s'' ?
(I''m asking because I''m not entirely sure how the execute command is
doing the substitution)

-Dave

Hi Dave,

I''m not sure. I''ve been using this format for all of my other queries
without issue. What would the query look like with the bind variable
instead?

Erik


On Fri, 2007-05-25 at 09:51 -0500, Dave Borne wrote:

I''m trying to run the following query:

...

member_id=%s AND expire_date NOW() AND completed=1 AND (product_id


Shouldn''t you be using the bind variable ''?'' instead of ''%s'' ?

The parameter placeholder for MySQLdb is, indeed and unfortunately, %s.
The OP is using parameter substitution correctly, though in an
obfuscated fashion. ''sql'' is a misnamed tuple containing both the query
string *and* the parameters, which is being unpacked with ''*'' into two
arguments to the execute call.

The only problem I see is that the parameters should be a sequence, i.e.
(self.uid,) instead of just (self.uid).

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net


这篇关于为什么这个查询不能在python中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆