473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why isn't this query working in python?

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

May 25 '07 #1
17 1737
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
May 25 '07 #2
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

May 25 '07 #3
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
May 25 '07 #4
On May 25, 11:28 am, Carsten Haese <cars...@uniqsys.comwrote:
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 Haesehttp://informixdb.sourceforge.net
I tried adding the comma to make it a sequence - but now change.

('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)', (1608L,))
()

What else could it be?

Thanks!
Erik

May 26 '07 #5
erikcw wrote:
On May 25, 11:28 am, Carsten Haese <cars...@uniqsys.comwrote:
>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 Haesehttp://informixdb.sourceforge.net

I tried adding the comma to make it a sequence - but now change.

('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)', (1608L,))
()

What else could it be?

Thanks!
Erik
It *could* be that there aren't any data meeting the criteria in your
query. Is there any chance that the current date/time has passed the
expire date since you ran the query manually?

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 26 '07 #6
On Sat, 2007-05-26 at 12:25 -0700, erikcw wrote:
I tried adding the comma to make it a sequence - but now change.

('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)', (1608L,))
()

What else could it be?
We won't know until you post more information. Show us the create table
statement for amember_payments, show us the query you're running
manually and the results you're getting from running the query manually.
Maybe that'll give us a clue. (Include the member_id, expire_date,
completed, and product_id columns in your manual query, please.)

--
Carsten Haese
http://informixdb.sourceforge.net
May 26 '07 #7
On May 27, 5:25 am, erikcw <erikwickst...@gmail.comwrote:
On May 25, 11:28 am, Carsten Haese <cars...@uniqsys.comwrote:
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 Haesehttp://informixdb.sourceforge.net

I tried adding the comma to make it a sequence - but now change.

('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)', (1608L,))
()

What else could it be?
Possibly a type mismatch. How is member_id declared in the CREATE
TABLE? For diagnostic purposes, try passing in (1608,) and ('1608',).

May 27 '07 #8
On May 26, 8:21 pm, John Machin <sjmac...@lexicon.netwrote:
On May 27, 5:25 am, erikcw <erikwickst...@gmail.comwrote:
On May 25, 11:28 am, Carsten Haese <cars...@uniqsys.comwrote:
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 Haesehttp://informixdb.sourceforge.net
I tried adding the comma to make it a sequence - but now change.
('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)', (1608L,))
()
What else could it be?

Possibly a type mismatch. How is member_id declared in the CREATE
TABLE? For diagnostic purposes, try passing in (1608,) and ('1608',).
Here is a copy of the table schema and the first 2 rows.

-- phpMyAdmin SQL Dump
-- version 2.9.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 27, 2007 at 11:29 AM
-- Server version: 5.0.27
-- PHP Version: 4.4.2
--
-- Database: `lybp_lybp`
--

-- --------------------------------------------------------

--
-- Table structure for table `amember_payments`
--

CREATE TABLE `amember_payments` (
`payment_id` int(11) NOT NULL auto_increment,
`member_id` int(11) NOT NULL default '0',
`product_id` int(11) NOT NULL default '0',
`begin_date` date NOT NULL default '0000-00-00',
`expire_date` date NOT NULL default '0000-00-00',
`paysys_id` varchar(32) NOT NULL default '',
`receipt_id` varchar(32) NOT NULL default '',
`amount` decimal(12,2) NOT NULL default '0.00',
`completed` smallint(6) default '0',
`remote_addr` varchar(15) NOT NULL default '',
`data` text,
`time` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
`aff_id` int(11) NOT NULL default '0',
`payer_id` varchar(255) NOT NULL default '',
`coupon_id` int(11) NOT NULL default '0',
`tm_added` datetime NOT NULL default '0000-00-00 00:00:00',
`tm_completed` datetime default NULL,
`tax_amount` decimal(12,2) NOT NULL default '0.00',
PRIMARY KEY (`payment_id`),
KEY `member_id` (`member_id`),
KEY `payer_id` (`payer_id`),
KEY `coupon_id` (`coupon_id`),
KEY `tm_added` (`tm_added`,`product_id`),
KEY `tm_completed` (`tm_completed`,`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11020 ;

--
-- Dumping data for table `amember_payments`
--

INSERT INTO `amember_payments` VALUES (423, 107, 1, '2004-10-01',
'2004-10-21', 'authorize_aim', '5687944', 3.95, 1, '', NULL,
'2004-11-30 19:21:43', 0, '', 0, '2004-11-30 19:21:43', '2004-11-30
19:21:43', 0.00);
INSERT INTO `amember_payments` VALUES (422, 107, 1, '2004-10-22',
'2004-11-21', 'authorize_aim', '5873225', 9.95, 1, '', NULL,
'2004-11-30 19:22:18', 0, '', 0, '2004-11-30 19:20:13', '2004-11-30
19:20:13', 0.00);

Thanks for your help!
Erik

May 27 '07 #9
Steve Howell wrote:
--- erikcw <er***********@gmail.comwrote:
>>>('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)', (1608L,))
()
Here is a copy of the table schema and the first 2
rows.

Does your table actually contain any rows that meet
the criteria that expire_date is in the future,
completed is 1, product id is between 11 and 21, etc.?

Have you tried debugging the SQL outside of Python?
This thread all started because a manual query was claimed to succeed
when a Python-based one was claimed not to.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 27 '07 #10
erikcw wrote:
On May 26, 8:21 pm, John Machin <sjmac...@lexicon.netwrote:
>On May 27, 5:25 am, erikcw <erikwickst...@gmail.comwrote:
>>On May 25, 11:28 am, Carsten Haese <cars...@uniqsys.comwrote:
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 Haesehttp://informixdb.sourceforge.net
I tried adding the comma to make it a sequence - but now change.
('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)', (1608L,))
()
What else could it be?
Possibly a type mismatch. How is member_id declared in the CREATE
TABLE? For diagnostic purposes, try passing in (1608,) and ('1608',).

Here is a copy of the table schema and the first 2 rows.

-- phpMyAdmin SQL Dump
-- version 2.9.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 27, 2007 at 11:29 AM
-- Server version: 5.0.27
-- PHP Version: 4.4.2
--
-- Database: `lybp_lybp`
--

-- --------------------------------------------------------

--
-- Table structure for table `amember_payments`
--

CREATE TABLE `amember_payments` (
`payment_id` int(11) NOT NULL auto_increment,
`member_id` int(11) NOT NULL default '0',
`product_id` int(11) NOT NULL default '0',
`begin_date` date NOT NULL default '0000-00-00',
`expire_date` date NOT NULL default '0000-00-00',
`paysys_id` varchar(32) NOT NULL default '',
`receipt_id` varchar(32) NOT NULL default '',
`amount` decimal(12,2) NOT NULL default '0.00',
`completed` smallint(6) default '0',
`remote_addr` varchar(15) NOT NULL default '',
`data` text,
`time` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
`aff_id` int(11) NOT NULL default '0',
`payer_id` varchar(255) NOT NULL default '',
`coupon_id` int(11) NOT NULL default '0',
`tm_added` datetime NOT NULL default '0000-00-00 00:00:00',
`tm_completed` datetime default NULL,
`tax_amount` decimal(12,2) NOT NULL default '0.00',
PRIMARY KEY (`payment_id`),
KEY `member_id` (`member_id`),
KEY `payer_id` (`payer_id`),
KEY `coupon_id` (`coupon_id`),
KEY `tm_added` (`tm_added`,`product_id`),
KEY `tm_completed` (`tm_completed`,`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11020 ;

--
-- Dumping data for table `amember_payments`
--

INSERT INTO `amember_payments` VALUES (423, 107, 1, '2004-10-01',
'2004-10-21', 'authorize_aim', '5687944', 3.95, 1, '', NULL,
'2004-11-30 19:21:43', 0, '', 0, '2004-11-30 19:21:43', '2004-11-30
19:21:43', 0.00);
INSERT INTO `amember_payments` VALUES (422, 107, 1, '2004-10-22',
'2004-11-21', 'authorize_aim', '5873225', 9.95, 1, '', NULL,
'2004-11-30 19:22:18', 0, '', 0, '2004-11-30 19:20:13', '2004-11-30
19:20:13', 0.00);

Thanks for your help!
Erik
I feel obliged to point out that there ARE no rows meeting the criteria
you query specified!

mysqlSELECT expire_date, NOW() FROM amember_payments;
+-------------+---------------------+
| expire_date | NOW() |
+-------------+---------------------+
| 2004-10-21 | 2007-05-27 15:59:21 |
| 2004-11-21 | 2007-05-27 15:59:21 |
+-------------+---------------------+
2 rows in set (0.02 sec)

mysql>

So I am not sure how you managed to get a manual query to work, but do
be sure that the Python query you mentioned at the start of the thread

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)
doesn't stand a chance of returning any results unless you use a time
machine to go back almost three years!

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 27 '07 #11

On May 27, 2007, at 4:01 PM, Steve Holden wrote:
erikcw wrote:
>On May 26, 8:21 pm, John Machin <sjmac...@lexicon.netwrote:
>>On May 27, 5:25 am, erikcw <erikwickst...@gmail.comwrote:

On May 25, 11:28 am, Carsten Haese <cars...@uniqsys.comwrote:
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 Haesehttp://informixdb.sourceforge.net
I tried adding the comma to make it a sequence - but now change.
('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)', (1608L,))
()
What else could it be?
Possibly a type mismatch. How is member_id declared in the CREATE
TABLE? For diagnostic purposes, try passing in (1608,) and
('1608',).

Here is a copy of the table schema and the first 2 rows.

-- phpMyAdmin SQL Dump
-- version 2.9.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 27, 2007 at 11:29 AM
-- Server version: 5.0.27
-- PHP Version: 4.4.2
--
-- Database: `lybp_lybp`
--

-- --------------------------------------------------------

--
-- Table structure for table `amember_payments`
--

CREATE TABLE `amember_payments` (
`payment_id` int(11) NOT NULL auto_increment,
`member_id` int(11) NOT NULL default '0',
`product_id` int(11) NOT NULL default '0',
`begin_date` date NOT NULL default '0000-00-00',
`expire_date` date NOT NULL default '0000-00-00',
`paysys_id` varchar(32) NOT NULL default '',
`receipt_id` varchar(32) NOT NULL default '',
`amount` decimal(12,2) NOT NULL default '0.00',
`completed` smallint(6) default '0',
`remote_addr` varchar(15) NOT NULL default '',
`data` text,
`time` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
`aff_id` int(11) NOT NULL default '0',
`payer_id` varchar(255) NOT NULL default '',
`coupon_id` int(11) NOT NULL default '0',
`tm_added` datetime NOT NULL default '0000-00-00 00:00:00',
`tm_completed` datetime default NULL,
`tax_amount` decimal(12,2) NOT NULL default '0.00',
PRIMARY KEY (`payment_id`),
KEY `member_id` (`member_id`),
KEY `payer_id` (`payer_id`),
KEY `coupon_id` (`coupon_id`),
KEY `tm_added` (`tm_added`,`product_id`),
KEY `tm_completed` (`tm_completed`,`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11020 ;

--
-- Dumping data for table `amember_payments`
--

INSERT INTO `amember_payments` VALUES (423, 107, 1, '2004-10-01',
'2004-10-21', 'authorize_aim', '5687944', 3.95, 1, '', NULL,
'2004-11-30 19:21:43', 0, '', 0, '2004-11-30 19:21:43', '2004-11-30
19:21:43', 0.00);
INSERT INTO `amember_payments` VALUES (422, 107, 1, '2004-10-22',
'2004-11-21', 'authorize_aim', '5873225', 9.95, 1, '', NULL,
'2004-11-30 19:22:18', 0, '', 0, '2004-11-30 19:20:13', '2004-11-30
19:20:13', 0.00);

Thanks for your help!
Erik
I feel obliged to point out that there ARE no rows meeting the
criteria
you query specified!

mysqlSELECT expire_date, NOW() FROM amember_payments;
+-------------+---------------------+
| expire_date | NOW() |
+-------------+---------------------+
| 2004-10-21 | 2007-05-27 15:59:21 |
| 2004-11-21 | 2007-05-27 15:59:21 |
+-------------+---------------------+
2 rows in set (0.02 sec)

mysql>

So I am not sure how you managed to get a manual query to work, but do
be sure that the Python query you mentioned at the start of the thread

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)

And doesn't the above comma, need to be a percent symbol?

Dave

doesn't stand a chance of returning any results unless you use a time
machine to go back almost three years!

regards
Steve

May 27 '07 #12
On May 27, 4:01 pm, Steve Holden <s...@holdenweb.comwrote:
erikcw wrote:
On May 26, 8:21 pm, John Machin <sjmac...@lexicon.netwrote:
On May 27, 5:25 am, erikcw <erikwickst...@gmail.comwrote:
>On May 25, 11:28 am, Carsten Haese <cars...@uniqsys.comwrote:
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 Haesehttp://informixdb.sourceforge.net
I tried adding the comma to make it a sequence - but now change.
('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)', (1608L,))
()
What else could it be?
Possibly a type mismatch. How is member_id declared in the CREATE
TABLE? For diagnostic purposes, try passing in (1608,) and ('1608',).
Here is a copy of the table schema and the first 2 rows.
-- phpMyAdmin SQL Dump
-- version 2.9.0.2
--http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 27, 2007 at 11:29 AM
-- Server version: 5.0.27
-- PHP Version: 4.4.2
--
-- Database: `lybp_lybp`
--
-- --------------------------------------------------------
--
-- Table structure for table `amember_payments`
--
CREATE TABLE `amember_payments` (
`payment_id` int(11) NOT NULL auto_increment,
`member_id` int(11) NOT NULL default '0',
`product_id` int(11) NOT NULL default '0',
`begin_date` date NOT NULL default '0000-00-00',
`expire_date` date NOT NULL default '0000-00-00',
`paysys_id` varchar(32) NOT NULL default '',
`receipt_id` varchar(32) NOT NULL default '',
`amount` decimal(12,2) NOT NULL default '0.00',
`completed` smallint(6) default '0',
`remote_addr` varchar(15) NOT NULL default '',
`data` text,
`time` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
`aff_id` int(11) NOT NULL default '0',
`payer_id` varchar(255) NOT NULL default '',
`coupon_id` int(11) NOT NULL default '0',
`tm_added` datetime NOT NULL default '0000-00-00 00:00:00',
`tm_completed` datetime default NULL,
`tax_amount` decimal(12,2) NOT NULL default '0.00',
PRIMARY KEY (`payment_id`),
KEY `member_id` (`member_id`),
KEY `payer_id` (`payer_id`),
KEY `coupon_id` (`coupon_id`),
KEY `tm_added` (`tm_added`,`product_id`),
KEY `tm_completed` (`tm_completed`,`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11020 ;
--
-- Dumping data for table `amember_payments`
--
INSERT INTO `amember_payments` VALUES (423, 107, 1, '2004-10-01',
'2004-10-21', 'authorize_aim', '5687944', 3.95, 1, '', NULL,
'2004-11-30 19:21:43', 0, '', 0, '2004-11-30 19:21:43', '2004-11-30
19:21:43', 0.00);
INSERT INTO `amember_payments` VALUES (422, 107, 1, '2004-10-22',
'2004-11-21', 'authorize_aim', '5873225', 9.95, 1, '', NULL,
'2004-11-30 19:22:18', 0, '', 0, '2004-11-30 19:20:13', '2004-11-30
19:20:13', 0.00);
Thanks for your help!
Erik

I feel obliged to point out that there ARE no rows meeting the criteria
you query specified!

mysqlSELECT expire_date, NOW() FROM amember_payments;
+-------------+---------------------+
| expire_date | NOW() |
+-------------+---------------------+
| 2004-10-21 | 2007-05-27 15:59:21 |
| 2004-11-21 | 2007-05-27 15:59:21 |
+-------------+---------------------+
2 rows in set (0.02 sec)

mysql>

So I am not sure how you managed to get a manual query to work, but do
be sure that the Python query you mentioned at the start of the thread

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)

doesn't stand a chance of returning any results unless you use a time
machine to go back almost three years!

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------
The rows I posted are just a small sample (the first 2). There are
tens of thousands of rows in the table.

Also, yes, the query does work when I run it manually against MySQL.

May 27 '07 #13
On Sun, 2007-05-27 at 16:39 -0400, da******@mac.com wrote:
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)


And doesn't the above comma, need to be a percent symbol?
No. The poster is using parameter passing, not string formatting.

--
Carsten Haese
http://informixdb.sourceforge.net
May 28 '07 #14
da******@mac.com wrote:
>
On May 27, 2007, at 4:01 PM, Steve Holden wrote:
>erikcw wrote:
>>On May 26, 8:21 pm, John Machin <sjmac...@lexicon.netwrote:
On May 27, 5:25 am, erikcw <erikwickst...@gmail.comwrote:

On May 25, 11:28 am, Carsten Haese <cars...@uniqsys.comwrote:
>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 Haesehttp://informixdb.sourceforge.net
I tried adding the comma to make it a sequence - but now change.
('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)', (1608L,))
()
What else could it be?
Possibly a type mismatch. How is member_id declared in the CREATE
TABLE? For diagnostic purposes, try passing in (1608,) and ('1608',).

Here is a copy of the table schema and the first 2 rows.

-- phpMyAdmin SQL Dump
-- version 2.9.0.2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 27, 2007 at 11:29 AM
-- Server version: 5.0.27
-- PHP Version: 4.4.2
--
-- Database: `lybp_lybp`
--

-- --------------------------------------------------------

--
-- Table structure for table `amember_payments`
--

CREATE TABLE `amember_payments` (
`payment_id` int(11) NOT NULL auto_increment,
`member_id` int(11) NOT NULL default '0',
`product_id` int(11) NOT NULL default '0',
`begin_date` date NOT NULL default '0000-00-00',
`expire_date` date NOT NULL default '0000-00-00',
`paysys_id` varchar(32) NOT NULL default '',
`receipt_id` varchar(32) NOT NULL default '',
`amount` decimal(12,2) NOT NULL default '0.00',
`completed` smallint(6) default '0',
`remote_addr` varchar(15) NOT NULL default '',
`data` text,
`time` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
`aff_id` int(11) NOT NULL default '0',
`payer_id` varchar(255) NOT NULL default '',
`coupon_id` int(11) NOT NULL default '0',
`tm_added` datetime NOT NULL default '0000-00-00 00:00:00',
`tm_completed` datetime default NULL,
`tax_amount` decimal(12,2) NOT NULL default '0.00',
PRIMARY KEY (`payment_id`),
KEY `member_id` (`member_id`),
KEY `payer_id` (`payer_id`),
KEY `coupon_id` (`coupon_id`),
KEY `tm_added` (`tm_added`,`product_id`),
KEY `tm_completed` (`tm_completed`,`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11020 ;

--
-- Dumping data for table `amember_payments`
--

INSERT INTO `amember_payments` VALUES (423, 107, 1, '2004-10-01',
'2004-10-21', 'authorize_aim', '5687944', 3.95, 1, '', NULL,
'2004-11-30 19:21:43', 0, '', 0, '2004-11-30 19:21:43', '2004-11-30
19:21:43', 0.00);
INSERT INTO `amember_payments` VALUES (422, 107, 1, '2004-10-22',
'2004-11-21', 'authorize_aim', '5873225', 9.95, 1, '', NULL,
'2004-11-30 19:22:18', 0, '', 0, '2004-11-30 19:20:13', '2004-11-30
19:20:13', 0.00);

Thanks for your help!
Erik
I feel obliged to point out that there ARE no rows meeting the criteria
you query specified!

mysqlSELECT expire_date, NOW() FROM amember_payments;
+-------------+---------------------+
| expire_date | NOW() |
+-------------+---------------------+
| 2004-10-21 | 2007-05-27 15:59:21 |
| 2004-11-21 | 2007-05-27 15:59:21 |
+-------------+---------------------+
2 rows in set (0.02 sec)

mysql>

So I am not sure how you managed to get a manual query to work, but do
be sure that the Python query you mentioned at the start of the thread

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)


And doesn't the above comma, need to be a percent symbol?
Nope.
Dave

>doesn't stand a chance of returning any results unless you use a time
machine to go back almost three years!

regards
Steve



--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------
May 28 '07 #15
En Mon, 28 May 2007 14:53:57 -0300, Dennis Lee Bieber
<wl*****@ix.netcom.comescribió:
On Sun, 27 May 2007 20:35:28 -0400, Carsten Haese <ca*****@uniqsys.com>
declaimed the following in comp.lang.python:
>On Sun, 2007-05-27 at 16:39 -0400, da******@mac.com wrote:
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)

It's confusing, as the example shown is not the common form for
parameter passing on the .execute() call -- without the .execute() it is
unclear. I presume the .execute() is using *sql to unpack the tuple...
Yes, the original message said self.amember_cursor.execute(*sql)
It IS confusing...

--
Gabriel Genellina

May 28 '07 #16
On May 28, 2:47 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Mon, 28 May 2007 14:53:57 -0300, Dennis Lee Bieber
<wlfr...@ix.netcom.comescribió:
On Sun, 27 May 2007 20:35:28 -0400, Carsten Haese <cars...@uniqsys.com>
declaimed the following in comp.lang.python:
On Sun, 2007-05-27 at 16:39 -0400, davel...@mac.com wrote:
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)
It's confusing, as the example shown is not the common form for
parameter passing on the .execute() call -- without the .execute() it is
unclear. I presume the .execute() is using *sql to unpack the tuple...

Yes, the original message said self.amember_cursor.execute(*sql)
It IS confusing...

--
Gabriel Genellina
This is how I've always writing my queries. I learned it from some
tutorial I found on Google when I started - what is the preferred/
pythonic way to write this query?

Thanks!
Erik

May 29 '07 #17
erikcw wrote:
On May 28, 2:47 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>En Mon, 28 May 2007 14:53:57 -0300, Dennis Lee Bieber
<wlfr...@ix.netcom.comescribió:
>>On Sun, 27 May 2007 20:35:28 -0400, Carsten Haese <cars...@uniqsys.com>
declaimed the following in comp.lang.python:
On Sun, 2007-05-27 at 16:39 -0400, davel...@mac.com wrote:
> 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)
It's confusing, as the example shown is not the common form for
parameter passing on the .execute() call -- without the .execute() it is
unclear. I presume the .execute() is using *sql to unpack the tuple...
Yes, the original message said self.amember_cursor.execute(*sql)
It IS confusing...

--
Gabriel Genellina

This is how I've always writing my queries. I learned it from some
tutorial I found on Google when I started - what is the preferred/
pythonic way to write this query?
Use separate names for the SQL statement and the argument tuple, then
pass them as two separate arguments to cursor.execute().

Apart from anything else this makes it somewhat easier to use the same
statement with different arguments. But it is no different in principle
than what you are doing now, so the change won't remove your bug, I'm
afraid.

Could you show us a query (with actual results, if you can publish them)
that works manually but doesn't work in Python? There has to be
something pretty simple wrong here.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get on the web: Blog, lens and tag your way to fame!!
holdenweb.blogspot.com squidoo.com/pythonology
tagged items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

May 29 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: mythis | last post by:
Hey guys, I seem to be having a problem with part of my code. This code is supposed to connect to a database and perform a query to get all the name fields in all the records. The program then...
8
by: Gregory Piñero | last post by:
Hi guys, I'm trying to run this statement: os.system(r'"C:\Program Files\Mozilla Firefox\firefox.exe"' + ' "www.blendedtechnologies.com"') The goal is to have firefox open to that website....
2
by: muttu2244 | last post by:
Hi all, 1) Am working on windows XP, and is there any way i can get the whole name as "windows Xp" using python script? i have tried with "os.sys.platform" but it just gives me as "win32",...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.