0
for toolx in tools:
    print toolx

output_count = """SELECT count(tool),count(""SELECT count(tool) FROM 
               core_data WHERE sentiment = 'positive' AND tool = '%s'"") 
               from core_data where tool = '%s'""" % (toolx, toolx)

I don't know how to compare against strings while running multiple sql statements in a query.

1
  • Please fix indentation. Commented Feb 25, 2018 at 12:43

1 Answer 1

1

You have couple of issues with your code.

First, you should use place holders for parameters and pass parameters as tuple to cursor when executing SQL query. This way MySQL will guard you from SQL injection attacks to some extent.

Example:

cursor.execute("SELECT * FROM core_data WHERE sentiment = 'positive' AND tool = %s",  (toolx,))

Second issue is when you do the counting of all positive sentiments. You would be much better with:

SELECT count(*) as 'All count', 
       sum(case when sentiment = 'positive' then 1 else 0 end) as 'Positive Sentiments'
FROM core_data 
WHERE tool = %s

To wrap up, here is how it should look:

for toolx in tools:
    print toolx
    query = "SELECT count(*) as 'All count', sum(case when sentiment = 'positive' then 1 else 0 end) as 'Positive Sentiments' FROM core_data WHERE tool = %s"
    results = cursor.execute(query, (toolx,))
    # and then you do the fetchone and take counts
Sign up to request clarification or add additional context in comments.

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.