The below logic is used to convert so that list of strings such that each string is within quotes and comma separated
>>> record_hash
['AAA', 'BBB', 'CCC']
>>> t="'"+"','".join(record_hash)+"'"
>>> t
"'AAA','BBB','CCC'"
>>>
This logic is passed to below logic to form the SQL statement to form the where column in condition
def selectAllPackageType(record_hash, severity):
t="'"+"','".join(record_hash[])+"'"
joined_string = ', '.join(['"{}"'.format(value) for value in record_hash])
sql = 'select package_type from slack_jira where severity = ? and record_hash in (?)'
print(sql)
conn = getSQLConnection();
package_type = []
conn.set_trace_callback(print)
if conn is not None:
cursor = conn.cursor()
cursor.execute(sql,(severity,t))
records = cursor.fetchall()
for row in records:
package_type.append(row['package_type'])
cursor.close()
conn.close()
else:
print("Error! cannot perform selectByPackageType.")
return package_type
The conn.set_trace_callback(print) prints this value which is causing to fail the fetch the record as it contains one single record
select package_type from slack_jira where severity = 'MEDIUM' and record_hash in ('''AAAAA'',''CCCC''');
what is needed is to form the correct SQL statement to be created with proper escape for where column in (?)
select package_type from slack_jira where severity = 'MEDIUM' and record_hash in ('AAAAA','CCCC');
t="'"+"','".join(record_hash[])+"'"should be giving you aSyntaxError. You also never usejoined_string? Beyond that, it might help if you printedtandrecord_hashjust to confirm that they contain what you think they should.t=",".join(record_hash)?placeholder. You cannot insert a dynamic amount of variables in there. The?is sanitized and only accepts "one string value". --- as in your sql statement needs to have multiple?and then you just unpack*record_hashsqlto have ending... record_hash in ?i.e. replace(?)with?plus changetto bet = tuple(record_hash).