3

So, I've a table (say table_name ) and it columns are:

|  A   |  B   |  C   |
------------------------
|  a1  |  b1  |  c1  |
|  a2  |  b2  |  c2  |
   ...

Now i have to read a column data using following query:

import pandas as pd
import pymysql as pycon

con = pycon.connect(host='localhost',user='root',password='', db='database')

#Error in here
df= pd.read_sql_query("SELECT A from table_name where B = %s and C != %s" %variableB %variableC, con)

But I've encountered an error in read_sql_query(...) maybe the query format is wrong because dynamically binded single parameter works fine i.e

df= pd.read_sql_query("SELECT A from table_name where B = %s" %variableB, con)

works w/o error. Can anyone help me with the query?

1
  • 1
    Try df = pd.read_sql_query("SELECT A from table_name where B = %s and C != %s" % (variableB, variableC), con) Commented Aug 6, 2017 at 8:26

2 Answers 2

3

In case someone ran into same problem, the correct code for query was with a '%s' instead of %s

df= pd.read_sql_query("SELECT A from table_name where B = '%s' and C != '%s'" % (variableB, variableC), con)

the code below gave me pymysql.err.InternalError.

df= pd.read_sql_query("SELECT A from table_name where B = %s and C != %s" % (variableB, variableC), con)

Thank you stackOverflow :)

Sign up to request clarification or add additional context in comments.

Comments

1

when you bind your multiple variable into String, syntax should be like this

df= pd.read_sql_query("SELECT A from table_name where B = %s and C != %s" % (variableB, variableC), con)

1 Comment

It gave me pymysql.err.InternalError working now. All i had to do was replace %s with '%s' in your code. I have posted a solution as answer below. Thanks for your help

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.