0

I'm looking to take an array list and attach it to a string.

Python 2.7.10, Windows 10

The list is loaded from a mySQL table and the output is this:

skuArray = [('000381001238',) ('000381001238',) ('000381001238',) ('FA200513652',) ('000614400967',)]

I'm wanting to take this list and attach it to a separate query

the problem:

query = "SELECT ItemLookupCode,Description, Quantity, Price, LastReceived "
query = query+"FROM Item "
query = query+"WHERE ItemLookupCode IN ("+skuArray+") " 
query = query+"ORDER BY LastReceived ASC;"

I get the error:

TypeError: cannot concatenate 'str' and 'tuple' objects

My guess here is that I need to format the string as:

'000381001238', '000381001238', '000381001238', 'FA200513652','000614400967'

Ultimately the string needs to read:

query = query+"WHERE ItemLookupCode IN ('000381001238', '000381001238', '000381001238', 'FA200513652','000614400967') "

I have tried the following:

skuArray = ''.join(skuArray.split('(', 1))
skuArray = ''.join(skuArray.split(')', 1))

Second Try:

skus = [sku[0] for sku in skuArray]
stubs = ','.join(["'?'"]*len(skuArray))


msconn = pymssql.connect(host=r'*', user=r'*', password=r'*', database=r'*')
cur = msconn.cursor()
query ='''
SELECT ItemLookupCode,Description, Quantity, Price, LastReceived
FROM Item 
WHERE ItemLookupCode IN { sku_params }
ORDER BY LastReceived ASC;'''.format(sku_params = stubs)
cur.execute(query, params=skus)
row = cur.fetchone()
print row[3]
cur.close()
msconn.close()

Thanks in advance for your help!

8
  • 1
    don't format variables into SQL-Statements. Use placeholders. Commented Jan 12, 2017 at 16:02
  • Yeah, I do realize the SQL injection aspect.. I'll give that shot. Thanks! Commented Jan 12, 2017 at 16:03
  • 1
    "and the output is this" Are you sure? That does not look like the usual Python representation for either a tuple or a list. Looks more like a tuple of one-elemented tuples, but not exactly... Please show the exact output, with all the brackets and commas, and possibly also tell us what type(skuArray) shown. Commented Jan 12, 2017 at 16:03
  • yes. that was output when I printed the list. Commented Jan 12, 2017 at 16:05
  • 1
    """select ItemLookupCode,Description, Quantity, Price, LastReceived from item where ItemLookupCode in {} order by LastReceived ASC;""".format(tuple(x for x in skuArray)). Commented Jan 12, 2017 at 16:07

2 Answers 2

4

If you want to do the straight inline SQL you could use a list comprehension:

', '.join(["'{}'}.format(sku[0]) for sku in skuArray])

Note: You need to add commas between tuples (based on example)

That said, if you want to do some sql, I would encourage you to parameterize your request with ?

Here is an example of how you would do something like that:

skuArray = [('000381001238',), ('000381001238',), ('000381001238',), ('FA200513652',), ('000614400967',)]
skus = [sku[0] for sku in skuArray]
stubs = ','.join(["'?'"]*len(skuArray))

qry = '''
SELECT ItemLookupCode,Description, Quantity, Price, LastReceived 
FROM Item 
WHERE ItemLookupCode IN ({ sku_params })
ORDER BY LastReceived ASC;'''.format(sku_params = stubs)

#assuming pyodbc connection syntax may be off
conn.execute(qry, params=skus)

Why?

Non-parameterized queries are a bad idea because it leaves you vulnerable to sql injection and is easy to avoid.

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

10 Comments

Thank you for your help.
Thanks again - However I get an error because there is no second comma: ('000381001238',), any ideas here?
Edit: I have updated my question with this code (cleaner)
Do you need to have it as a tuple or can you just straight value search? What error are you getting?
Traceback.. ", line 60, in <module> ORDER BY LastReceived ASC;'''.format(sku_params = stubs) KeyError: ' sku_params '
|
2

Assuming that skuArray is a list, like this:

>>> skuArray = [('000381001238',), ('000381001238',), ('000381001238',), ('FA200513652',), ('000614400967',)]

You can format your string like this:

>>> ', '.join(["'{}'".format(x[0]) for x in skuArray])
"'000381001238', '000381001238', '000381001238', 'FA200513652', '000614400967'"

2 Comments

You are making the assumption that skuArray is a list.
Yes, this is a list. Thank you.

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.