Skip to content

Instantly share code, notes, and snippets.

@tjmtmmnk
Last active December 17, 2019 15:59
Show Gist options
  • Save tjmtmmnk/2058db9a654315c0c9d79326bbb0ef72 to your computer and use it in GitHub Desktop.
Save tjmtmmnk/2058db9a654315c0c9d79326bbb0ef72 to your computer and use it in GitHub Desktop.
bind sql parameter
sql_text = """
select * from a
where x = ?
and y = ?
"""
bind_values_text = """1, 2"""
sql = sql_text.replace("\n", " ")
_bind_values = bind_values_text.replace(' ', '')
bind_values = _bind_values.split(",")
param_num = sql.count('?')
if len(bind_values) != param_num:
exit("not matched bind values num")
for value in bind_values:
insert_value = "'" + value + "'"
sql = sql.replace('?', insert_value, 1)
print(sql)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment