1

I have 2 tuples a and b

print(a)
(1,2,3,4,5)

print(b)
(1,)

As you can see the second one has a single value... For the first one I use

query_a = 'SELECT something FROM mytable WHERE id IN {};'.format(a)

It works perfectly.

If I do the same for b, it doesn't work. It shows:

Error: blah blah blah ..WHERE id IN (1,);': (1064, "You have an error in your SQL syntax...

I believe the error is because of the tuple comma -> (1,) How do I solve this. I tried

query_b = 'SELECT something FROM mytable WHERE id IN ();'.format(b)

but it also doesn't work. How do I fix this in one line? Thanks.

1
  • Are you sure b is a tuple, not a string? How do you create it? Commented Jan 19, 2022 at 22:14

2 Answers 2

2

You can do some simple string manipulations to remove the comma.

'Tuple without comma: {}'.format(str(b)[:-2] + str(b)[-1])

This should work regardless of the tuple length.

Also as a side note, the {} is part of syntax for Format Strings in Python. You cannot replace that with () to make it to work.

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

Comments

0

Thanks. It works. To make it more clear the answer is:

query_a = 'SELECT something FROM mytable WHERE id IN {};'.format(str(a)[:-2] + str(a)[-1])

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.