Database query count
<p>I am trying to create a program that will iterate through a data base to match a query for "_75" and set the count to 0 when that match is made. For every records in a database that does not match the query I would like for it to accumulate a negative 1 count. No matter how I try the code I get either the total record count or the total records that match the query. </p>

<pre><code>import sqlite3

conn = sqlite3.connect('p34.db')

c = conn.cursor()
q = "SELECT * FROM 'Pick 3'"
z =  "SELECT * FROM 'Pick 3' WHERE Number LIKE '_75' ORDER BY Draw DESC;"

c.execute (q)
rez = c.fetchall()
count = 0
for row in rez:

if row == rez:
count = 0
else:
count = count -1

print (count)
conn.close()
</code></pre>

<p>my example for desired results for query _75.</p>

<pre><code>record 1 075 count 0
record 2 223 count -1
record 3 228 count -2
record 4 323 count -3
record 5 275 count 0
record 6 888 count -1
</code></pre>

<p>Change your query to return a count rather than counting in the python code
For example</p>

<pre><code>SELECT COUNT(CustomerID) AS OrdersFromCustomerID7 FROM Orders WHERE CustomerID=7;
</code></pre>

<p><a href="http://www.w3schools.com/sql/sql_func_count.asp" rel="nofollow">Source</a></p>

<p>Query to count the number of rows where the field is like _75 and also query where fields are not like _75. Take one number from the other. Or just count how many entries are in the table.</p>

python python-3.x sqlite3