0

I am running the below code:

db = pymysql.connect(host=host, database=db_name, user=user, password=password)
batchsize = 100
for offset in range(0,1000,batchsize):
     df = pd.read_sql(('SELECT * FROM anime LIMIT %s OFFSET %s', (batchsize,offset)), con=db)
     print("rows and columns: ",df.shape)

But it is throwing the following error in line 4:

can't concat tuple to bytes

Any suggestion is welcome.

1
  • Show the full traceback. Commented Jan 16, 2018 at 10:40

1 Answer 1

1

You have wrongly formatted sql string with tuple.

Change:

df = pd.read_sql(('SELECT * FROM anime LIMIT %s OFFSET %s', (batchsize,offset)), con=db)

To:

df = pd.read_sql(('SELECT * FROM anime LIMIT %s OFFSET %s' % (batchsize,offset)), con=db)

When you are binding values using tuple you must use % in between formatted string and tuples.

>>> k = "uid"
>>> v = "sa"
>>> "%s=%s", (k, v)  
('%s=%s', ('uid', 'sa'))  
>>>   
>>> "%s=%s" % (k, v)
'uid=sa'

The whole expression evaluates to a string.
The first %s is replaced by the value of k;
the second %s is replaced by the value of v.
All other characters in the string (in this case, the equal sign) stay as they are.
Note that (k, v) is a tuple.

Read:

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

1 Comment

Thank a lot Ravinder.. :-)

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.