I have a snippet in my code that needs to find a mac address in the database, then print a statement to show what row it's been found in. I will then use that row number to update the timestamp column in that row.
My sqlite3 DB looks like this:
CREATE TABLE IF NOT EXISTS My_TABLENAME (mac_addr TEXT, timestamp TEXT, location TEXT, serialno TEXT)"
The code looks like this.
import sqlite3 as lite
con = lite.connect("database.db")
con.row_factory = lite.Row
cur = con.cursor()
cur.execute('''SELECT mac_addr, timestamp, (SELECT COUNT(*) FROM MY_TABLENAME AS t2 WHERE t2.mac_addr <= t1.mac_addr) AS i FROM My_TABLENAME AS t1 ORDER BY timestamp, mac_addr''')
_data = cur.fetchone()
if str(_data[0]) != '5c:f5:da:e0:dd:44':
cur.fetchone()
else:
print "Found the device in row", str(_data[2])
when I run the code in the python cli I get the below when I print the _data variable after doing the first fetchone()
>>> _data
(u'34:0a:ff:b2:75:78', u'1433940972.03946', 135)
>>>
So I can see that the row currently in the _data variable is row 135 (this changes obviously).
But when I run the code snippet I posted uptop I get the following output, which makes me think the loop is not working. Am I doing something wrong?
>>> import sqlite3 as lite
>>> con = lite.connect("database.db")
>>> con.row_factory = lite.Row
>>> cur = con.cursor()
>>> cur.execute('''SELECT mac_addr, timestamp, (SELECT COUNT(*) FROM My_TABLENAME AS t2 WHERE t2.mac_addr <= t1.mac_addr) AS i FROM My_TABLENAME AS t1 ORDER BY timestamp, mac_addr''')
<sqlite3.Cursor object at 0x7ff99fc0a8f0>
>>> _data = cur.fetchone()
>>> if str(_data[0]) != '5c:f5:da:e0:dd:44':
... cur.fetchone()
... else:
... print "Found the device in row", str(_data[2])
...
(u'18:83:31:61:83:8c', u'1433940974.39824', 74)
>>>
Any advice would be great please.
Aucun commentaire:
Enregistrer un commentaire