Python SQL query using variables
<pre><code>#Delete suspense window
class dWindow(QtGui.QMainWindow, Ui_dWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)
        self.setupUi(self)

        for row in cursor.execute("SELECT FIRSTNAME FROM Staff"):
            self.comboUser.addItems(row)
        con.close()

        self.btnDeleteSuspense.clicked.connect(self.btnDeleteSuspense_Clicked)

    def btnDeleteSuspense_Clicked(self):
        user = self.comboUser.currentText() #finds selected user
        date = self.dateEdit.date().toString("M/d/yyyy")
        numrecord = cursor.execute() ??
</code></pre>

<p>Here is a sample DB and program file to further help me explain</p>

<p><img src="http://i.stack.imgur.com/sYTN5.png" alt="programsample"></p>

<p><img src="http://i.stack.imgur.com/BL5XF.png" alt="dbsample"></p>

<p>I have created variables to hold the selection of the combobox and the dateEdit box.</p>

<p>The next step (the one I'm struggling with) is to then use those variables in an SQL query that will first find the count of rows with the selected user name and having a date &lt;= than the selected date.  That will populate the numrecord variable so that I can display a "This will delete 'x' rows, are you sure?"</p>

<p>If the user selects yes then I will then use the variable in a delete query to delete the selected rows.</p>

<p>I believe if I can figure out how to use the variables I have in a SQL query then the DELETE query should be rather simple.</p>

<p>An example of a possible DELETE query to show what I'm trying to do</p>

<pre><code>cursor.execute("DELETE TO, count(*) FROM Suspense where TO = [user] and DATE = [date]")
</code></pre>

<p>I know that is wrong but maybe it will help clarify.</p>

<p>I hope I have explained my question fully and I appreciate any help provided.</p>

<p>Edit: Thanks so much!!</p>

<p>Just before I saw that you had posted this I figured it out.</p>

<p>What I came up with was the following:</p>

<pre><code>qdate = self.dateTimeEdit.dateTime().toPyDateTime() #grabs the raw datetime from the QDateTimeEdit object and converts to python datetime

query = "SELECT DATE FROM Suspense WHERE DATE &gt;= ?"  #creates the query using ? as a placeholder for variable

cursor.execute(query, (qdate,)) #executes the query and passes qdate as a tuple to the placeholder
</code></pre>

<p>With this knowledge I can recreate my queries to include both variables.</p>

<p>As mentioned in a comment to another answer, you should be using a proper <em>parameterized query</em>, for example:</p>

<pre class="lang-python prettyprint-override"><code># assumes that autocommit=False (the default)
crsr = conn.cursor()
sql = "DELETE FROM [Suspense] WHERE [TO]=? AND [DATE]&lt;=?"
user = self.comboUser.currentText()  # as before
date = self.dateEdit.date()  # Note: no .toString(...) required
params = (user, date)
crsr.execute(sql, params)
msg = "About to delete {} row(s). Proceed?".format(crsr.rowcount)
if my_confirmation_dialog(msg):
    conn.commit()
else:
    conn.rollback()
</code></pre>

python sql pyqt pyodbc pypyodbc