lundi 4 janvier 2016

Building sqlite queries in Python using string literal concatenation [duplicate]

I'm trying to build some SQLite queries using Python's string literal concatenation. I'm using the sqlite3 module. Here's an example of a query:

 cur.execute('SELECT * FROM DB.TBL1 '
             'WHERE (?) = (?) '
             'AND DB.TBL1.Day BETWEEN (?) AND (?) ',
             ('DB.TBL1.'.join(user_id), user, arr[0], arr[-1]))

In other words, I want the question marks to act as placeholders and substitute in the values on the last line of the query.

I have two questions with this...

  • Will the variables be substituted correctly using the ? this way?
  • The first substitution uses a Python variable to build the identifier (e.g., if user_id = 'ABC', then 'DB.TBL1.'.join(user_id) would become 'DB.TBL1.ABC. Is this the most appropriate way to do this?

Thank you for any help/suggestions.

Aucun commentaire:

Enregistrer un commentaire