0

We have a script that was running comfortably for past 4 years. It pulls a lot of data from a SugarCRM application and then prints some reports.

I am using Python-MySQLDB to pull the data.

  db = mdb.connect('localhost', db_user, db_pass, db_name)
  c = db.cursor()
  statement = "
select a.email_address
     , c.hits
     , c.activity_date
     , c.activity_type 
  from email_addresses a
     , campaign_log c
     , email_addr_bean_rel r
 where c.target_id = r.bean_id 
   and r.email_address_id = a.id 
   and c.activity_type = 'targeted' 
   and c.campaign_id ='%s'
"%x
  c.execute(statement)
  rows = c.fetchall()

Problem is the SQL query gets executed both on MySQL shell as well as on phpmyadmin throwing in correct values as results.

But on python shell

>>rows
>>()

Except that the SugarCRM version was updated a few weeks back, and even after post updating things were working fine.

What could be the problem? Is it that new version have some InnoDB tables?

I am stumped. Any guidance would be welcome.

Edit-- I had copied a SQL Query statement which executes correctly on both mysql shell as well as phpmyadmin window, and ran it, and results are the same. It is showing an empty tuple, and no SQL records are available.

I am certain the issue is not because of passing the string, as that is the direction which everyone seems to be suggesting.

6
  • what is %x at the end of your statement? Commented May 22, 2015 at 15:55
  • @haifzhan: String interpolation. Commented May 22, 2015 at 16:02
  • @IgnacioVazquez-Abrams oh yes, why not use it? Commented May 22, 2015 at 16:03
  • 2
    @haifzhan: Using string interpolation instead of a parametrized query allows for the possibility of a SQL injection attack. Commented May 22, 2015 at 16:04
  • To clarify the command c.fetchall() even after I substitute the value for x with a value. Is there an issue with long running or complex SQL queries with mysql-python Commented May 22, 2015 at 17:03

2 Answers 2

2

If your SQL query need a parameter "c.campaign_id ='%s'", you should specify it in "c.execute(statement)", e.g.:

c.execute(statement, (42))
Sign up to request clarification or add additional context in comments.

2 Comments

"42" is example value, that means you query records with c.campaign_id =42. And "%x" after closing ' " ' not needed.
I was thinking it must be a tuple, I tried yours, it works :)
0

I figured out the issue. I upgraded python-MySql version to the latest. It at least managed to tell me that there is an error with one of the MySQL tables. Repaired MySQL Table, and everything is working fine.

For anyone who faces similar issue it is advisable to install latest MySQL-python using pip. Make sure you install before you pip install.

sudo apt-get install libmysqlclient-dev python-dev

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.